wxPythonのスレッド

グラフィカルインターフェイスを使用してPythonでプログラムを作成する場合、データのさまざまな長期処理を開始する必要がある場合があります。ほとんどの場合、インターフェイスはブロックされ、ユーザーにはプログラムがフリーズします。 これを回避するには、タスクを並列スレッドまたはプロセスで実行する必要があります。 この記事では、Threadingモジュールを使用してwxPythonでこれを行う方法について説明します。



スレッドセーフなwxPythonメソッド



wxPythonでスレッドを操作するには、3つの方法があります。 これらを使用しないと、インターフェースを更新するときにPythonプログラムがフリーズする場合があります。 これを回避するには、スレッドセーフメソッドwx.PostEvent、wx.CallAfter、およびwx.CallLaterを使用する必要があります。 Robin Dunn(wxPythonの作成者)によると、wx.CallAfterはwx.PostEventを使用してアプリケーションオブジェクトにイベントを送信します。 アプリケーションにはこのイベントのハンドラーがあり、設定されたアルゴリズムに従って応答します。 私が理解している限りでは、wx.CallLaterは、指定された時間パラメーターでwx.CallAfterを呼び出します。これにより、イベントを送信する前に待機する量がわかります。



また、Robin Dunnは、Global Interpreter Lock(GIL)では複数のスレッドを同時に実行できないため、使用するプロセッサコアの数が制限される可能性があることに注意しました。 一方、彼はまた、wxライブラリのAPI関数を呼び出すことにより、wxPythonがGILから解放されるため、他のスレッドが同時に動作できると述べました。 つまり、マルチコアマシンでスレッドを使用すると、パフォーマンスが変化する可能性があります。 この問題の議論は興味深く、明確ではないかもしれません...

ご注意 perev。 -GILの詳細については、 こちらをご覧ください



3つのメソッドは抽象化のレベルに分けることができます。wx.CallLaterが最上位にあり、次にwx.CallAfterとwx.PostEventが最下位にあります。 次の例では、WxPythonプログラムでwx.CallAfterとwx.PostEventを使用する方法を示します。



wxPython、スレッド、wx.CallAfter、PubSub



wxPythonメーリングリストでは、専門家が他のユーザーに、wx.CallAfterをPubSubで使用して、アプリケーションとスレッドの間でメッセージを交換するように指示していることがわかります。 次の例では、これを示します。

Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  1. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  2. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  3. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  4. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  5. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  6. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  7. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  8. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  9. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  10. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  11. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  12. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  13. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  14. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  15. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  16. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  17. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  18. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  19. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  20. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  21. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  22. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  23. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  24. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  25. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  26. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  27. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  28. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  29. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  30. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  31. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  32. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  33. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  34. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  35. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  36. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  37. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  38. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  39. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  40. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  41. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  42. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  43. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  44. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  45. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  46. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  47. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  48. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  49. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  50. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  51. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  52. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  53. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  54. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  55. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  56. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  57. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  58. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  59. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  60. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  61. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  62. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  63. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()



  64. Copy Source | Copy HTML import time import wx from threading import Thread from wx.lib.pubsub import Publisher ######################################################################## class TestThread (Thread): """Test Worker Thread Class.""" #---------------------------------------------------------------------- def __init__ (self): """Init Worker Thread Class.""" Thread. __init__ (self) self .start() # start the thread #---------------------------------------------------------------------- def run (self): """Run Worker Thread.""" # This is the code executing in the new thread. for i in range ( 6 ): time .sleep( 10 ) wx.CallAfter( self . postTime , i) time .sleep( 5 ) wx.CallAfter(Publisher().sendMessage, "update" , "Thread finished!" ) #---------------------------------------------------------------------- def postTime (self, amt): """ <br/> Send time to GUI <br/> """ amtOfTime = (amt + 1 ) * 10 Publisher().sendMessage( "update" , amtOfTime) ######################################################################## class MyForm (wx.Frame): #---------------------------------------------------------------------- def __init__ (self): wx.Frame. __init__ (self, None, wx.ID_ANY, "Tutorial" ) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) self .displayLbl = wx.StaticText(panel, label= "Amount of time since thread started goes here" ) self .btn = btn = wx.Button(panel, label= "Start Thread" ) btn.Bind(wx.EVT_BUTTON, self . onButton ) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .displayLbl, 0 , wx.ALL|wx.CENTER, 5 ) sizer.Add(btn, 0 , wx.ALL|wx.CENTER, 5 ) panel.SetSizer(sizer) # create a pubsub receiver Publisher().subscribe( self . updateDisplay , "update" ) #---------------------------------------------------------------------- def onButton (self, event): """ <br/> Runs the thread <br/> """ TestThread () self .displayLbl.SetLabel( "Thread started!" ) btn = event.GetEventObject() btn.Disable() #---------------------------------------------------------------------- def updateDisplay (self, msg): """ <br/> Receives data from thread and updates the display <br/> """ t = msg.data if isinstance (t, int): self .displayLbl.SetLabel( "Time since thread started: %s seconds" % t) else : self .displayLbl.SetLabel( "%s" % t) self .btn.Enable() #---------------------------------------------------------------------- # Run the program if __name__ == "__main__" : app = wx.PySimpleApp() frame = MyForm ().Show() app.MainLoop()





この例では、timeモジュールを使用して、長時間実行される偽のプロセスを作成します。 ただし、代わりにもっと便利なものを使用できます。 実際の例では、ストリームを使用してAdobe Readerを開き、PDFを送信して印刷します。 この操作は取るに足らないように見えるかもしれませんが、スレッドを使用しない場合、アプリケーションの印刷ボタンは、ドキュメントがプリンターに送信されるまで押されたままになり、インターフェイスはアクションが完了するまでフリーズされたままです。 1、2秒でもユーザーにとって注目に値します!



とにかく、それがどのように機能するか見てみましょう。 スレッドクラス(以下で説明)では、必要に応じて「run」メソッドを再定義しました。 __init__メソッドに「self.start()」があるため、このスレッドは作成時に開始されます。 「run」メソッドでは、10秒ごとに6回ループし、wx.CallAfterとPubSubを使用してインターフェイスを更新します。 サイクルが完了すると、最終メッセージをアプリケーションに送信して、ユーザーに通知します。



Copy Source | Copy HTML



  1. ######################################################### ########################
  2. クラス TestThread (スレッド):
  3. "" "テストワーカースレッドクラス。" ""
  4. #------------------------------------------------- ---------------------
  5. def __init__ (self):
  6. "" "初期ワーカースレッドクラス。" ""
  7. スレッド __init__ (自己)
  8. self .start() #スレッドを開始
  9. #------------------------------------------------- ---------------------
  10. def run (self):
  11. "" "ワーカースレッドを実行します。" ""
  12. #これは、新しいスレッドで実行されるコードです。
  13. 範囲内の i( 6 ):
  14. 時間の睡眠( 10
  15. wx.CallAfter( self。postTime 、i)
  16. 時間の睡眠( 5
  17. wx.CallAfter(Publisher()。sendMessage、 "update""Thread finished!"
  18. #------------------------------------------------- ---------------------
  19. def postTime (self、amt):
  20. "" " <br/> GUIに時間を送信 <br/> " ""
  21. amtOfTime =(amt + 1 )* 10
  22. パブリッシャー()。SendMessage( "update" 、amtOfTime)




このコードでは、ボタンを使用してスレッドが開始されます。 同時に、誤って追加のスレッドを開始しないように非アクティブにします。そうしないと、異なるスレッドからのさまざまな乱雑なメッセージが表示され、混乱を招くだけです。 それはうまく使用することができますが。 ストリームのPIDを表示して、誰が誰であるかを確認し、スクロール行に情報を表示して、複数のストリームの動作を一度に観察できます。



コードの最後の興味深い部分は、イベントとイベントハンドラーを受け入れるPubSubです。



Copy Source | Copy HTML



  1. def updateDisplay (self、msg):
  2. "" " <br/> スレッドからデータを受信し、表示を更新します <br/> " ""
  3. t = msg.data
  4. isinstance (t、int)の場合:
  5. self .displayLbl.SetLabel( "スレッドが開始されてからの時間:%s秒" %t)
  6. その他
  7. self .displayLbl.SetLabel( "%s" %t)
  8. self .btn.Enable()




そのため、ストリームからメッセージを抽出し、インターフェイスを更新します。 その際、メッセージのデータ型を確認して、ユーザーに正確に表示する内容を決定します。 1つ下のレベルに移動して、wx.PostEventで同じことを試してみましょう。



ストリームとwx.PostEvent



以下のコードは、このwxPython wikiの例に基づいています 。 このコードは、前述のコードよりも少し複雑ですが、理解するのは難しくないと思います。



Copy Source | Copy HTML



  1. 輸入時間
  2. wxをインポート
  3. スレッド化インポートスレッドから
  4. #スレッド完了の通知イベントを定義する
  5. EVT_RESULT_ID = wx.NewId()
  6. def EVT_RESULT (win、func):
  7. "" "結果イベントを定義します。" ""
  8. win.Connect( -1-1 、EVT_RESULT_ID、func)
  9. クラス ResultEvent (wx.PyEvent):
  10. "" "任意の結果データを運ぶ単純なイベント。" ""
  11. def __init__ (自己、データ):
  12. "" "初期結果イベント。" ""
  13. wx.PyEvent。 __init__ (自己)
  14. self .SetEventType(EVT_RESULT_ID)
  15. self .data = data
  16. ######################################################### ########################
  17. クラス TestThread (スレッド):
  18. "" "テストワーカースレッドクラス。" ""
  19. #------------------------------------------------- ---------------------
  20. def __init__(自己、wxObject):
  21. "" "初期ワーカースレッドクラス。" ""
  22. スレッド __init__ (自己)
  23. self .wxObject = wxObject
  24. self .start() #スレッドを開始
  25. #------------------------------------------------- ---------------------
  26. def run (self):
  27. "" "ワーカースレッドを実行します。" ""
  28. #これは、新しいスレッドで実行されるコードです。
  29. 範囲内の i( 6 ):
  30. 時間の睡眠( 10
  31. amtOfTime =(i + 1 )* 10
  32. wx.PostEvent( self .wxObject、 ResultEvent (amtOfTime))
  33. 時間の睡眠( 5
  34. wx.PostEvent( self .wxObject、 ResultEvent"Thread finished!" ))
  35. ######################################################### ########################
  36. クラス MyForm (wx.Frame):
  37. #------------------------------------------------- ---------------------
  38. def __init__ (self):
  39. wx.Frame。 __init__(self、None、wx.ID_ANY、 「チュートリアル」
  40. #パネルを追加して、すべてのプラットフォームで正しく見えるようにする
  41. panel = wx.Panel(自己、wx.ID_ANY)
  42. self .displayLbl = wx.StaticText(パネル、ラベル= 「スレッドが開始されてからの時間がここに行く」
  43. self .btn = btn = wx.Button(パネル、ラベル= "開始スレッド"
  44. btn.Bind(wx.EVT_BUTTON、 self。onButton
  45. sizer = wx.BoxSizer(wx.VERTICAL)
  46. sizer.Add( self .displayLbl、 0 、wx.ALL | wx.CENTER、 5
  47. sizer.Add(btn、 0 、wx.ALL | wx.CENTER、 5
  48. panel.SetSizer(サイザー)
  49. #ワーカースレッドの結果のイベントハンドラーを設定する
  50. EVT_RESULT (self、 self。UpdateDisplay
  51. #------------------------------------------------- ---------------------
  52. def onButton (self、event):
  53. "" " <br/> スレッドを実行 <br/> " ""
  54. TestThread (自己)
  55. self .displayLbl.SetLabel( "スレッドが開始されました!"
  56. btn = event.GetEventObject()
  57. btn.Disable()
  58. #------------------------------------------------- ---------------------
  59. def updateDisplay (self、msg):
  60. "" " <br/> スレッドからデータを受信し、表示を更新します <br/> " ""
  61. t = msg.data
  62. isinstance (t、int)の場合:
  63. self .displayLbl.SetLabel( "スレッドが開始されてからの時間:%s秒" %t)
  64. その他
  65. self .displayLbl.SetLabel( "%s" %t)
  66. self .btn.Enable()
  67. #------------------------------------------------- ---------------------
  68. #プログラムを実行する
  69. __name__ == "__main__"の場合
  70. app = wx.PySimpleApp()
  71. frame = MyForm ().Show()
  72. app.MainLoop()




少し見てみましょう。 最初の3つの部分は、私にとって最も混乱しやすいものです。



Copy Source | Copy HTML



  1. #スレッド完了の通知イベントを定義する
  2. EVT_RESULT_ID = wx.NewId()
  3. def EVT_RESULT (win、func):
  4. "" "結果イベントを定義します。" ""
  5. win.Connect( -1-1 、EVT_RESULT_ID、func)
  6. クラス ResultEvent (wx.PyEvent):
  7. "" "任意の結果データを運ぶ単純なイベント。" ""
  8. def __init__ (自己、データ):
  9. "" "初期結果イベント。" ""
  10. wx.PyEvent。 __init__ (自己)
  11. self .SetEventType(EVT_RESULT_ID)
  12. self .data = data




ここではEVT_RESULT_IDがキーであり、EVT_RESULT関数をResultEventクラスに関連付けているようです。 EVT_RESULT関数を使用して、ストリームが生成するイベントハンドラーをセットアップします。 wx.PostEvent関数は、イベントをストリームからResultEventクラスにルーティングし、それは以前にインストールされたイベントハンドラーによって処理されます。



TestThreadクラスは、PubSubの代わりにwx.PostEventを使用したことを除いて、以前とほぼ同じように機能します。 インターフェイスを更新するイベントハンドラーのコードは変更されていません。



おわりに



wxPythonプログラムで基本的なフローメソッドを使用する方法を知っていることを願っています。 スレッドを操作する方法は他にもいくつかありますが、wx.YieldやQueuesなど、この記事では考慮していません。 幸いなことに、wxPython wikiはこれらのトピックを非常によくカバーしているので、これらの方法に興味があるなら、以下のリンクを必ずチェックしてください。



追加資料






All Articles