3 Run wxPython in a second thread.
6 Importing this module creates a second thread and starts
7 wxPython in that thread. Its single method,
8 add_cone(), sends an event to the second thread
9 telling it to create a VTK viewer window with a cone in
12 This module is meant to be imported into the standard
13 Python interpreter. It also works with Pythonwin.
14 It doesn't seem to work with IDLE (on NT anyways).
15 It should also work in a wxPython application.
17 Applications already running a wxPython app do not
18 need to start a second thread. In these cases,
19 viewer creates the cone windows in the current
20 thread. You can test this by running shell.py
21 that comes with wxPython, importing viewer and
26 Python 1.5.2 (#1, Sep 17 1999, 20:15:36) ...
27 Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
29 >>> viewer.add_cone() # pop up a cone window
32 >>> viewer.add_cone() # create another cone window
34 Why would anyone do this?:
35 When using wxPython, the call to app.Mainloop() takes over
36 the thread from which it is called. This presents a
37 problem for applications that want to use the standard
38 Python command line user interface, while occasionaly
39 creating a GUI window for viewing an image, plot, etc.
40 One soultion is to mangage the GUI in a second thread.
42 wxPython does not behave well if windows are created in
43 a thread other than the one where wxPython was originally
44 imported. ( I assume importing wxPython initializes some
45 info in the thread). The current solution is to make the
46 original import of wxPython in the second thread and then
47 create all windows in that second thread.
49 Methods in the main thread can create a new window by issuing
50 events to a "catcher" window in the second thread. This
51 catcher window has event handlers that actually create the
57 """ start the GUI thread
60 thread
.start_new_thread(self
.run
, ())
64 Note that viewer_basices is first imported ***here***.
65 This is the second thread. viewer_basics imports
66 wxPython. if we imported it at
67 the module level instead of in this function,
68 the import would occur in the main thread and
69 wxPython wouldn't run correctly in the second thread.
71 from viewer_basics
import *
73 self
.app
= SecondThreadApp(0)
80 send an event to the catcher window in the
81 other thread and tell it to create a cone window.
85 evt
= viewer_basics
.AddCone()
86 viewer_basics
.wxPostEvent(self
.app
.catcher
, evt
)
88 viewer_basics
.add_cone()
90 viewer
= viewer_thread()