]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/viewer.py
added SetString(), deprecated SetLabel()
[wxWidgets.git] / wxPython / demo / viewer.py
1 #!/usr/bin/env python
2 """
3 Run wxPython in a second thread.
4
5 Overview:
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
10 it.
11
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.
16
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
22 calling add_cone.
23
24 Usage:
25 [user]$ python
26 Python 1.5.2 (#1, Sep 17 1999, 20:15:36) ...
27 Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
28 >>> import viewer
29 >>> viewer.add_cone() # pop up a cone window
30 >>> a = 1
31 1
32 >>> viewer.add_cone() # create another cone window
33
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.
41
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.
48
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
52 new window.
53 """
54
55 class viewer_thread:
56 def start(self):
57 """ start the GUI thread
58 """
59 import thread,time
60 thread.start_new_thread(self.run, ())
61
62 def run(self):
63 """
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.
70 """
71 from viewer_basics import *
72 try:
73 self.app = SecondThreadApp(0)
74 self.app.MainLoop()
75 except TypeError:
76 self.app = None
77
78 def add_cone(self):
79 """
80 send an event to the catcher window in the
81 other thread and tell it to create a cone window.
82 """
83 import viewer_basics
84 if self.app:
85 evt = viewer_basics.AddCone()
86 viewer_basics.wxPostEvent(self.app.catcher, evt)
87 else:
88 viewer_basics.add_cone()
89
90 viewer = viewer_thread()
91 viewer.start()
92
93 def add_cone():
94 viewer.add_cone()
95
96