]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/tests/test8.py
   1 # Thread testing example. Harm van der Heijden, March 26 1999. 
   3 # Rule One in threading: make sure only one thread interacts with the 
   4 # user interface. See the wxTextCtrlQueue class for an example of how 
   9 from   whrandom 
import random
 
  11 from wxPython
.wx 
import * 
  13 # Set this to zero to prevent entering the wxApp mainloop 
  14 # (for testing whether threads work at all in the absense of wxWindows) 
  17 # write a message to stdout every second 
  20         sleeptime 
= (random() * 3) + 0.5 
  21         print "Hello from %s (%1.3f)" % (mesg
, sleeptime
) 
  24 # the same, but write it to a textctrl. 
  25 def DoTextCtrlThread(text
, mesg
): 
  27         sleeptime 
= (random() * 3) + 0.5 
  28         text
.WriteText("Hello from %s (%1.3f)\n" % (mesg
, sleeptime
)) 
  31 # A very simple queue for textctrls. 
  32 # Nice demonstration of the power of OO programming too (at least I think so!) 
  33 # WriteText puts text in the queue, rather than writing it immediately. 
  34 # The main (UI) thread must call Flush to force output. (see MyFrame::OnIdle) 
  35 class wxTextCtrlQueue(wxTextCtrl
): 
  36     def __init__(self
, parent
, id, value
, pos
, size
, flags
): 
  37         wxTextCtrl
.__init
__(self
,parent
, id, value
, pos
, size
, flags
) 
  39     def WriteText(self
, value
): 
  40         self
.queue
.append(value
) 
  45             wxTextCtrl
.WriteText(self
,value
) 
  47 # MyFrame and MyApp are very simple classes to test python threads in 
  49 class MyFrame(wxFrame
): 
  51         wxFrame
.__init
__(self
, NULL
, -1, "test threads", wxDefaultPosition
, wxSize(300,200)) 
  52         self
.text 
= wxTextCtrlQueue(self
, -1, "thread output\n", wxDefaultPosition
, wxDefaultSize
, wxTE_MULTILINE
) 
  54         menu
.Append(1001, "Start thread") 
  57         menubar
.Append(menu
, "Action") 
  58         self
.SetMenuBar(menubar
) 
  59         EVT_MENU(self
, 1001, self
.StartThread
) 
  60     def StartThread(self
, event
): 
  61         self
.cnt 
= self
.cnt 
+ 1 
  62         thread
.start_new_thread(DoTextCtrlThread
, (self
.text
, "thread %d" % self
.cnt
)) 
  63     def OnIdle(self
, event
): 
  69         self
.SetTopWindow(frame
) 
  73 # Start two threads that print a message every second 
  74 thread
.start_new_thread(DoThread
, ("thread A",)) 
  75 thread
.start_new_thread(DoThread
, ("thread B",)) 
  77 # if using wxpython, open a frame. Otherwise, just hang in while 1