スレッドセーフな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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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
- ######################################################### ########################
- クラス TestThread (スレッド):
- "" "テストワーカースレッドクラス。" ""
- #------------------------------------------------- ---------------------
- def __init__ (self):
- "" "初期ワーカースレッドクラス。" ""
- スレッド __init__ (自己)
- self .start() #スレッドを開始
- #------------------------------------------------- ---------------------
- def run (self):
- "" "ワーカースレッドを実行します。" ""
- #これは、新しいスレッドで実行されるコードです。
- 範囲内の i( 6 ):
- 時間の睡眠( 10 )
- wx.CallAfter( self。postTime 、i)
- 時間の睡眠( 5 )
- wx.CallAfter(Publisher()。sendMessage、 "update" 、 "Thread finished!" )
- #------------------------------------------------- ---------------------
- def postTime (self、amt):
- "" " <br/> GUIに時間を送信 <br/> " ""
- amtOfTime =(amt + 1 )* 10
- パブリッシャー()。SendMessage( "update" 、amtOfTime)
このコードでは、ボタンを使用してスレッドが開始されます。 同時に、誤って追加のスレッドを開始しないように非アクティブにします。そうしないと、異なるスレッドからのさまざまな乱雑なメッセージが表示され、混乱を招くだけです。 それはうまく使用することができますが。 ストリームのPIDを表示して、誰が誰であるかを確認し、スクロール行に情報を表示して、複数のストリームの動作を一度に観察できます。
コードの最後の興味深い部分は、イベントとイベントハンドラーを受け入れるPubSubです。
Copy Source | Copy HTML
- def updateDisplay (self、msg):
- "" " <br/> スレッドからデータを受信し、表示を更新します <br/> " ""
- t = msg.data
- isinstance (t、int)の場合:
- self .displayLbl.SetLabel( "スレッドが開始されてからの時間:%s秒" %t)
- その他 :
- self .displayLbl.SetLabel( "%s" %t)
- self .btn.Enable()
そのため、ストリームからメッセージを抽出し、インターフェイスを更新します。 その際、メッセージのデータ型を確認して、ユーザーに正確に表示する内容を決定します。 1つ下のレベルに移動して、wx.PostEventで同じことを試してみましょう。
ストリームとwx.PostEvent
以下のコードは、このwxPython wikiの例に基づいています 。 このコードは、前述のコードよりも少し複雑ですが、理解するのは難しくないと思います。
Copy Source | Copy HTML
- 輸入時間
- wxをインポート
- スレッド化インポートスレッドから
- #スレッド完了の通知イベントを定義する
- EVT_RESULT_ID = wx.NewId()
- def EVT_RESULT (win、func):
- "" "結果イベントを定義します。" ""
- win.Connect( -1 、 -1 、EVT_RESULT_ID、func)
- クラス ResultEvent (wx.PyEvent):
- "" "任意の結果データを運ぶ単純なイベント。" ""
- def __init__ (自己、データ):
- "" "初期結果イベント。" ""
- wx.PyEvent。 __init__ (自己)
- self .SetEventType(EVT_RESULT_ID)
- self .data = data
- ######################################################### ########################
- クラス TestThread (スレッド):
- "" "テストワーカースレッドクラス。" ""
- #------------------------------------------------- ---------------------
- def __init__(自己、wxObject):
- "" "初期ワーカースレッドクラス。" ""
- スレッド __init__ (自己)
- self .wxObject = wxObject
- self .start() #スレッドを開始
- #------------------------------------------------- ---------------------
- def run (self):
- "" "ワーカースレッドを実行します。" ""
- #これは、新しいスレッドで実行されるコードです。
- 範囲内の i( 6 ):
- 時間の睡眠( 10 )
- amtOfTime =(i + 1 )* 10
- wx.PostEvent( self .wxObject、 ResultEvent (amtOfTime))
- 時間の睡眠( 5 )
- wx.PostEvent( self .wxObject、 ResultEvent ( "Thread finished!" ))
- ######################################################### ########################
- クラス MyForm (wx.Frame):
- #------------------------------------------------- ---------------------
- def __init__ (self):
- wx.Frame。 __init__(self、None、wx.ID_ANY、 「チュートリアル」 )
- #パネルを追加して、すべてのプラットフォームで正しく見えるようにする
- panel = wx.Panel(自己、wx.ID_ANY)
- self .displayLbl = wx.StaticText(パネル、ラベル= 「スレッドが開始されてからの時間がここに行く」 )
- self .btn = btn = wx.Button(パネル、ラベル= "開始スレッド" )
- 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(サイザー)
- #ワーカースレッドの結果のイベントハンドラーを設定する
- EVT_RESULT (self、 self。UpdateDisplay )
- #------------------------------------------------- ---------------------
- def onButton (self、event):
- "" " <br/> スレッドを実行 <br/> " ""
- TestThread (自己)
- self .displayLbl.SetLabel( "スレッドが開始されました!" )
- btn = event.GetEventObject()
- btn.Disable()
- #------------------------------------------------- ---------------------
- def updateDisplay (self、msg):
- "" " <br/> スレッドからデータを受信し、表示を更新します <br/> " ""
- t = msg.data
- isinstance (t、int)の場合:
- self .displayLbl.SetLabel( "スレッドが開始されてからの時間:%s秒" %t)
- その他 :
- self .displayLbl.SetLabel( "%s" %t)
- self .btn.Enable()
- #------------------------------------------------- ---------------------
- #プログラムを実行する
- __name__ == "__main__"の場合 :
- app = wx.PySimpleApp()
- frame = MyForm ().Show()
- app.MainLoop()
少し見てみましょう。 最初の3つの部分は、私にとって最も混乱しやすいものです。
Copy Source | Copy HTML
- #スレッド完了の通知イベントを定義する
- EVT_RESULT_ID = wx.NewId()
- def EVT_RESULT (win、func):
- "" "結果イベントを定義します。" ""
- win.Connect( -1 、 -1 、EVT_RESULT_ID、func)
- クラス ResultEvent (wx.PyEvent):
- "" "任意の結果データを運ぶ単純なイベント。" ""
- def __init__ (自己、データ):
- "" "初期結果イベント。" ""
- wx.PyEvent。 __init__ (自己)
- self .SetEventType(EVT_RESULT_ID)
- self .data = data
ここではEVT_RESULT_IDがキーであり、EVT_RESULT関数をResultEventクラスに関連付けているようです。 EVT_RESULT関数を使用して、ストリームが生成するイベントハンドラーをセットアップします。 wx.PostEvent関数は、イベントをストリームからResultEventクラスにルーティングし、それは以前にインストールされたイベントハンドラーによって処理されます。
TestThreadクラスは、PubSubの代わりにwx.PostEventを使用したことを除いて、以前とほぼ同じように機能します。 インターフェイスを更新するイベントハンドラーのコードは変更されていません。
おわりに
wxPythonプログラムで基本的なフローメソッドを使用する方法を知っていることを願っています。 スレッドを操作する方法は他にもいくつかありますが、wx.YieldやQueuesなど、この記事では考慮していません。 幸いなことに、wxPython wikiはこれらのトピックを非常によくカバーしているので、これらの方法に興味があるなら、以下のリンクを必ずチェックしてください。
追加資料
- LongRunningTasks wikiページ
- ノンブロッキングGUI wikiページ
- WorkingWithThreads wikiページ