Some time ago I wrote an industrial control software, in use has always been a problem, is when the software retrieval equipment, because the function of the implementation of a long time, leading to the GUI animation, so that users can not tell whether the software is still in execution, or really hung up. (although I designed the sync log display, but this also suspended animation)
The screenshot of the program is as follows:
The code resolves as follows:
#-*-Coding:utf-8-*-
import time
import WX-
threading Import Thread from
wx.lib.pubsub import publi Sher
The time library is used to perform timed functions, simulating functions that require lengthy execution. Publisher is used to transfer messages between threads.
Class Testthread (Thread):
def __init__ (self):
#线程实例化时立即启动
thread.__init__ (self)
Self.start ()
def run (self):
#线程执行的代码 for
I in Range (a):
time.sleep (0.03)
WX. Callafter (Publisher (). SendMessage, "Update", i)
time.sleep (0.5)
wx. Callafter (Publisher (). SendMessage, "Update", U "Thread End")
Testthread's __init__ function, which starts when the thread instantiation is defined, and the Run function is a program that takes a long time to execute.
Wx. Callafter and publisher (). SendMessage are used to send messages to the GUI.
Class MyForm (WX. Frame): Def __init__ (self, parent): WX. Frame.__init__ (self, parent, id = wx.id_any, title = u "leniy,20140627", pos = wx. defaultposition, size = wx. Size ( -1,-1), style = WX. Default_frame_style|wx. Tab_traversal) self. Setsizehintssz (WX. DefaultSize, WX. defaultsize) GSizer2 = wx. Gridsizer (0, 3, 0, 0) self.m_button2 = wx. button (self, wx.id_any, U "Execute thread", WX. Defaultposition, WX. DefaultSize, 0 gsizer2.add (self.m_button2, 0, WX. All|wx. Align_center_horizontal|wx. Align_center_vertical, 5) self.m_statictext2 = WX. Statictext (self, Wx.id_any, U "MyLabel", WX. Defaultposition, WX. DefaultSize, 0) Self.m_staticText2.Wrap (-1) gsizer2.add (self.m_statictext2, 0, WX. All|wx. Align_center_horizontal|wx. Align_center_vertical, 5) Self.m_gauge1 = WX. Gauge (self, wx.id_any, WX.) Defaultposition, WX. DefaultSize, Wx.ga_horizontal) self.m_gauge1. SetValue (0) gsizer2.add (self.m_gauge1, 0, WX. All|wx. Align_center_horizontal|wx. Align_center_vertical, 5) Self. Setsizer (GSizer2) self. Layout () Gsizer2.fit (self) self. Centre (WX. BOTH) Self.m_button2. Bind (WX. Evt_button, Self.onbutton) Publisher (). Subscribe (Self.updatedisplay, "Update") def updatedisplay (self, msg): t = Msg . data if Isinstance (t, int): #如果是数字, indicating that the thread is executing, displaying the digital Self.m_staticText2.SetLabel ("%s%%"% t) self.m_gauge1. SetValue (t) Else: #否则线程未执行, turn the button back on Self.m_staticText2.SetLabel ("%s"% T) self.m_button2. Enable () def onbutton (Self, event): Testthread () Self.m_staticText2.SetLabel (U "thread Start") event. Geteventobject ().
Disable ()
This is the GUI's main program, and __init__ uses publisher (). Subscribe (Self.updatedisplay, "Update") to declare the way to get thread messages and display them.
if __name__ = = "__main__":
app = wx. Pysimpleapp ()
MyForm (None). Show ()
app. Mainloop ()
Finally, the window is MyForm (None). Show () shows, the program is complete.