]> git.saurik.com Git - wxWidgets.git/blobdiff - wxPython/demo/Threads.py
adjusting to changes in xti.h
[wxWidgets.git] / wxPython / demo / Threads.py
index 19b759aecec5edd8021c824363416a06c8339732..25440e5106a18ba75363c0804cc96f3b5d57bf25 100644 (file)
@@ -1,5 +1,6 @@
 
 from wxPython.wx import *
+from wxPython.lib import newevent
 
 import thread
 import time
@@ -7,18 +8,8 @@ from   whrandom import random
 
 #----------------------------------------------------------------------
 
-wxEVT_UPDATE_BARGRAPH = 25015
-
-def EVT_UPDATE_BARGRAPH(win, func):
-    win.Connect(-1, -1, wxEVT_UPDATE_BARGRAPH, func)
-
-
-class UpdateBarEvent(wxPyEvent):
-    def __init__(self, barNum, value):
-        wxPyEvent.__init__(self)
-        self.SetEventType(wxEVT_UPDATE_BARGRAPH)
-        self.barNum = barNum
-        self.value = value
+# This creates a new Event class and a EVT binder function
+UpdateBarEvent, EVT_UPDATE_BARGRAPH = newevent.NewEvent()
 
 
 #----------------------------------------------------------------------
@@ -30,24 +21,23 @@ class CalcBarThread:
         self.val = val
 
     def Start(self):
-        self.keepGoing = self.running = true
+        self.keepGoing = self.running = True
         thread.start_new_thread(self.Run, ())
 
     def Stop(self):
-        self.keepGoing = false
+        self.keepGoing = False
 
     def IsRunning(self):
         return self.running
 
     def Run(self):
         while self.keepGoing:
-            evt = UpdateBarEvent(self.barNum, int(self.val))
+            evt = UpdateBarEvent(barNum = self.barNum, value = int(self.val))
             wxPostEvent(self.win, evt)
-            del evt
+            #del evt
 
             sleeptime = (random() * 2) + 0.5
-            #print self.barNum, 'sleeping for', sleeptime
-            time.sleep(sleeptime)
+            time.sleep(sleeptime/4)
 
             sleeptime = sleeptime * 5
             if int(random() * 2):
@@ -58,7 +48,7 @@ class CalcBarThread:
             if self.val < 0: self.val = 0
             if self.val > 300: self.val = 300
 
-        self.running = false
+        self.running = False
 
 #----------------------------------------------------------------------
 
@@ -71,11 +61,11 @@ class GraphWindow(wxWindow):
         for label in labels:
             self.values.append((label, 0))
 
-        self.font = wxFont(12, wxSWISS, wxNORMAL, wxBOLD)
-        self.SetFont(self.font)
+        font = wxFont(12, wxSWISS, wxNORMAL, wxBOLD)
+        self.SetFont(font)
 
         self.colors = [ wxRED, wxGREEN, wxBLUE, wxCYAN,
-                        wxNamedColour("Yellow"), wxNamedColor("Navy") ]
+                        "Yellow", "Navy" ]
 
         EVT_ERASE_BACKGROUND(self, self.OnEraseBackground)
         EVT_PAINT(self, self.OnPaint)
@@ -98,8 +88,12 @@ class GraphWindow(wxWindow):
         self.barHeight = hmax
 
 
+    def GetBestHeight(self):
+        return 2 * (self.barHeight + 1) * len(self.values)
+
+
     def Draw(self, dc, size):
-        dc.SetFont(self.font)
+        dc.SetFont(self.GetFont())
         dc.SetTextForeground(wxBLUE)
         dc.SetBackground(wxBrush(self.GetBackgroundColour()))
         dc.Clear()
@@ -156,23 +150,21 @@ class TestFrame(wxFrame):
         panel.SetFont(wxFont(10, wxSWISS, wxNORMAL, wxBOLD))
         wxStaticText(panel, -1,
                      "This demo shows multiple threads interacting with this\n"
-                     "window by sending events to it.", wxPoint(5,5))
+                     "window by sending events to it, one thread for each bar.",
+                     wxPoint(5,5))
         panel.Fit()
 
         self.graph = GraphWindow(self, ['Zero', 'One', 'Two', 'Three', 'Four',
                                         'Five', 'Six', 'Seven'])
+        self.graph.SetSize((450, self.graph.GetBestHeight()))
 
         sizer = wxBoxSizer(wxVERTICAL)
         sizer.Add(panel, 0, wxEXPAND)
         sizer.Add(self.graph, 1, wxEXPAND)
 
         self.SetSizer(sizer)
-        self.SetAutoLayout(true)
-
-        #self.graph.SetValue(0, 25)
-        #self.graph.SetValue(1, 50)
-        #self.graph.SetValue(2, 75)
-        #self.graph.SetValue(3, 100)
+        self.SetAutoLayout(True)
+        sizer.Fit(self)
 
         EVT_UPDATE_BARGRAPH(self, self.OnUpdate)
         self.threads = []
@@ -193,11 +185,12 @@ class TestFrame(wxFrame):
 
     def OnUpdate(self, evt):
         self.graph.SetValue(evt.barNum, evt.value)
-        self.graph.Refresh(false)
+        self.graph.Refresh(False)
 
 
     def OnCloseWindow(self, evt):
         busy = wxBusyInfo("One moment please, waiting for threads to die...")
+        wxYield()
         for t in self.threads:
             t.Stop()
         running = 1
@@ -215,7 +208,7 @@ class TestFrame(wxFrame):
 def runTest(frame, nb, log):
     win = TestFrame(frame, log)
     frame.otherWin = win
-    win.Show(true)
+    win.Show(True)
     return None
 
 #----------------------------------------------------------------------
@@ -230,7 +223,7 @@ so any cross platform GUI Toolkit and applications written with it
 need to take that into account.
 
 The solution is to only allow interaction with the GUI from a single
-thread, but this often severly limits what can be done in an
+thread, but this often severely limits what can be done in an
 application and makes it difficult to use additional threads at all.
 
 Since wxPython already makes extensive use of event handlers, it is a
@@ -242,3 +235,11 @@ ProcessEvent does, it processes it later from the context of the GUI
 thread.
 
 """
+
+
+
+if __name__ == '__main__':
+    import sys,os
+    import run
+    run.main(['', os.path.basename(sys.argv[0])])
+