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