]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/viewer.py
   4     Run wxPython in a second thread. 
   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 
  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. 
  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 
  27         Python 1.5.2 (#1, Sep 17 1999, 20:15:36)  ... 
  28         Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam 
  30         >>> viewer.add_cone() # pop up a cone window 
  33         >>> viewer.add_cone() # create another cone window 
  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. 
  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. 
  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 
  58         """ start the GUI thread 
  62         thread
.start_new_thread(self
.run
, ()) 
  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. 
  76             self
.app 
= viewer_basics
.SecondThreadApp(0) 
  83             send an event to the catcher window in the 
  84             other thread and tell it to create a cone window. 
  89             evt 
= viewer_basics
.AddCone() 
  90             viewer_basics
.wxPostEvent(self
.app
.catcher
, evt
) 
  92             viewer_basics
.add_cone() 
  94 viewer 
= viewer_thread()