]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/viewer.py
updates
[wxWidgets.git] / wxPython / demo / viewer.py
CommitLineData
f6bcfd97 1#!/usr/bin/env python
8fa876ca 2
f6bcfd97
BP
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
8b9a4190 39 Python command line user interface, while occasionally
f6bcfd97 40 creating a GUI window for viewing an image, plot, etc.
8b9a4190 41 One solution is to manage the GUI in a second thread.
f6bcfd97
BP
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
56class viewer_thread:
57 def start(self):
58 """ start the GUI thread
59 """
8fa876ca
RD
60 import time
61 import thread
f6bcfd97
BP
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 """
8fa876ca
RD
73 import viewer_basics
74
f6bcfd97 75 try:
8fa876ca 76 self.app = viewer_basics.SecondThreadApp(0)
f6bcfd97
BP
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 """
8fa876ca
RD
86 import viewer_basics
87
f6bcfd97
BP
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
94viewer = viewer_thread()
95viewer.start()
96
97def add_cone():
98 viewer.add_cone()
99
100