]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/viewer.py
Compatibility modules for a couple that have gone AWOL in the new namespace
[wxWidgets.git] / wxPython / demo / viewer.py
1 #!/usr/bin/env python
2 #
3 # 11/15/2003 - Jeff Grimmett (grimmtooth@softhome.net)
4 #
5 # o Updated for wx namespace
6 #
7
8 """
9 Run wxPython in a second thread.
10
11 Overview:
12 Importing this module creates a second thread and starts
13 wxPython in that thread. Its single method,
14 add_cone(), sends an event to the second thread
15 telling it to create a VTK viewer window with a cone in
16 it.
17
18 This module is meant to be imported into the standard
19 Python interpreter. It also works with Pythonwin.
20 It doesn't seem to work with IDLE (on NT anyways).
21 It should also work in a wxPython application.
22
23 Applications already running a wxPython app do not
24 need to start a second thread. In these cases,
25 viewer creates the cone windows in the current
26 thread. You can test this by running shell.py
27 that comes with wxPython, importing viewer and
28 calling add_cone.
29
30 Usage:
31 [user]$ python
32 Python 1.5.2 (#1, Sep 17 1999, 20:15:36) ...
33 Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
34 >>> import viewer
35 >>> viewer.add_cone() # pop up a cone window
36 >>> a = 1
37 1
38 >>> viewer.add_cone() # create another cone window
39
40 Why would anyone do this?:
41 When using wxPython, the call to app.Mainloop() takes over
42 the thread from which it is called. This presents a
43 problem for applications that want to use the standard
44 Python command line user interface, while occasionally
45 creating a GUI window for viewing an image, plot, etc.
46 One solution is to manage the GUI in a second thread.
47
48 wxPython does not behave well if windows are created in
49 a thread other than the one where wxPython was originally
50 imported. ( I assume importing wxPython initializes some
51 info in the thread). The current solution is to make the
52 original import of wxPython in the second thread and then
53 create all windows in that second thread.
54
55 Methods in the main thread can create a new window by issuing
56 events to a "catcher" window in the second thread. This
57 catcher window has event handlers that actually create the
58 new window.
59 """
60
61 class viewer_thread:
62 def start(self):
63 """ start the GUI thread
64 """
65 import time
66 import thread
67 thread.start_new_thread(self.run, ())
68
69 def run(self):
70 """
71 Note that viewer_basices is first imported ***here***.
72 This is the second thread. viewer_basics imports
73 wxPython. if we imported it at
74 the module level instead of in this function,
75 the import would occur in the main thread and
76 wxPython wouldn't run correctly in the second thread.
77 """
78 import viewer_basics
79
80 try:
81 self.app = viewer_basics.SecondThreadApp(0)
82 self.app.MainLoop()
83 except TypeError:
84 self.app = None
85
86 def add_cone(self):
87 """
88 send an event to the catcher window in the
89 other thread and tell it to create a cone window.
90 """
91 import viewer_basics
92
93 if self.app:
94 evt = viewer_basics.AddCone()
95 viewer_basics.wxPostEvent(self.app.catcher, evt)
96 else:
97 viewer_basics.add_cone()
98
99 viewer = viewer_thread()
100 viewer.start()
101
102 def add_cone():
103 viewer.add_cone()
104
105