多线程的界面操作,多线程界面操作,# coding=UTF


# coding=UTF-8  import wximport threadingimport randomclass WorkerTread(threading.Thread):        def __init__(self,threadNum,window):                threading.Thread.__init__(self)                self.threadNum = threadNum                self.window = window                self.timeToQuit = threading.Event()                self.timeToQuit.clear()                self.messageCount = random.randint(10,20)                self.messageDelay = 0.1 + 2.0*random.random()        def stop(self):                self.timeToQuit.set()        def run(self):                msg = u"线程 %d 执行 %d次 延迟 %1.4f\\n" %(self.threadNum,self.messageCount,self.messageDelay)                wx.CallAfter(self.window.LogMessage,msg)                for i in range(1,self.messageCount+1):                        self.timeToQuit.wait(self.messageDelay)                        if self.timeToQuit.isSet():                                break                        msg = u"信息 %d 来自于线程 %d\\n" % (i,self.threadNum)                        wx.CallAfter(self.window.LogMessage,msg)                else:                        wx.CallAfter(self.window.ThreadFinished,self)class MyFrame(wx.Frame):        def __init__(self):                wx.Frame.__init__(self,None,title=u"多线程演示界面")                self.threads = []                self.count = 0                Panel = wx.Panel(self)                startBtn = wx.Button(Panel,-1,u"开启一个线程")                stopBtn = wx.Button(Panel,-1,u"停止所有线程")                self.tc = wx.StaticText(Panel,-1,u"工作的线程:00")                self.log = wx.TextCtrl(Panel,-1,"",style = wx.TE_RICH|wx.TE_MULTILINE)                inner = wx.BoxSizer(wx.HORIZONTAL)                inner.Add(startBtn,0,wx.RIGHT,15)                inner.Add(stopBtn,0,wx.RIGHT,15)                inner.Add(self.tc,0,wx.ALIGN_CENTER_VERTICAL)                main = wx.BoxSizer(wx.VERTICAL)                main.Add(inner,0,wx.ALL,5)                main.Add(self.log,1,wx.EXPAND|wx.ALL,5)                Panel.SetSizer(main)                self.Bind(wx.EVT_BUTTON,self.OnStartButton,startBtn)                self.Bind(wx.EVT_BUTTON,self.OnStopButton,stopBtn)                self.Bind(wx.EVT_CLOSE,self.OnCloseWindow)                self.UpdateCount()        def OnStartButton(self,evt):                self.count +=1                thread = WorkerTread(self.count,self)                self.threads.append(thread)                self.UpdateCount()                thread.start()        def OnStopButton(self,evt):                self.StopTreads()                self.UpdateCount()        def OnCloseWindow(self,evt):                self.StopTreads()                self.Destroy()        def StopTreads(self):                while self.threads:                        thread = self.threads[0]                        thread.stop()                        self.threads.remove(thread)        def UpdateCount(self):                self.tc.SetLabel(u"工作线程: %d" % len(self.threads))        def LogMessage(self,msg):                self.log.AppendText(msg)        def ThreadFinished(self,thread):                self.threads.remove(thread)                self.UpdateCount()if __name__=='__main__':        app = wx.PySimpleApp()        frm = MyFrame()        frm.Show()        app.MainLoop()#该片段来自于http://byrx.net

评论关闭