wxPython使用Publisher实现进度条

  1 import time
  2 import wx
  3 from threading import Thread
  4 from wx.lib.pubsub import Publisher
  5 
  6 class ParseMML():
  7     souceFile=''
  8     targetFile=''
  9     progressNum=0
 10 
 11     def __init__(self):
 12         pass
 13 
 14     def run(self):
 15         for i in range(0,101,10):
 16             time.sleep(1)
 17             ParseMML.progressNum=i
 18             print(i)
 19 
 20 ########################################################################
 21 class TestThread(Thread):
 22     """Test Worker Thread Class."""
 23     #----------------------------------------------------------------------
 24     def __init__(self):
 25         """Init Worker Thread Class."""
 26         Thread.__init__(self)
 27         self.start()    # start the thread
 28     #----------------------------------------------------------------------
 29     def run(self):
 30         """Run Worker Thread."""
 31         # This is the code executing in the new thread.
 32         Thread(target=ParseMML().run).start()
 33         currentPro=0
 34         while True:
 35             pro = ParseMML.progressNum
 36             time.sleep(1)
 37             if pro==100:
 38                 break
 39             if pro>currentPro and pro <100:
 40                 wx.CallAfter(Publisher().sendMessage, "update", pro)
 41                 currentPro=pro
 42         wx.CallAfter(Publisher().sendMessage, "update", 100)
 43         wx.MessageBox("Done...", "message", wx.OK)
 44 
 45 ########################################################################
 46 class MyProgressDialog(wx.Dialog):
 47     """"""
 48     #----------------------------------------------------------------------
 49     def __init__(self):
 50         """Constructor"""
 51         wx.Dialog.__init__(self, None, title="Progress",size=(250,50))
 52         self.count = 0
 53         self.progress = wx.Gauge(self, range=100,size=(250,50))
 54         sizer = wx.BoxSizer(wx.VERTICAL)
 55 
 56         sizer.Add(self.progress, 0,wx.BOTTOM|wx.ALIGN_CENTER_VERTICAL,0)
 57         self.SetSizer(sizer)
 58         # create a pubsub listener
 59         Publisher().subscribe(self.updateProgress, "update")
 60     #----------------------------------------------------------------------
 61     def updateProgress(self, msg):
 62         """
 63         Update the progress bar
 64         """
 65         self.count = msg.data
 66         if self.count >= 100:
 67             self.Destroy()
 68 
 69         self.progress.SetValue(self.count)
 70 
 71 ########################################################################
 72 class MyFrame(wx.Frame):
 73     #----------------------------------------------------------------------
 74     def __init__(self):
 75         wx.Frame.__init__(self, None, title="Progress Bar ...")
 76         # Add a panel so it looks the correct on all platforms
 77         panel = wx.Panel(self, wx.ID_ANY)
 78         self.btn = btn = wx.Button(panel, label="Start Thread")
 79         btn.Bind(wx.EVT_BUTTON, self.onButton)
 80         sizer = wx.BoxSizer(wx.VERTICAL)
 81         sizer.Add(btn, 0, wx.ALL|wx.CENTER, 5)
 82         panel.SetSizer(sizer)
 83     #----------------------------------------------------------------------
 84     def onButton(self, event):
 85         """
 86         Runs the thread
 87         """
 88         btn = event.GetEventObject()
 89         btn.Disable()
 90         TestThread()
 91         dlg = MyProgressDialog()
 92         dlg.ShowModal()
 93         btn.Enable()
 94 #----------------------------------------------------------------------
 95 # Run the program
 96 if __name__ == "__main__":
 97     app = wx.App(False)
 98     frame = MyFrame()
 99     frame.Show()
100     app.MainLoop()

猜你喜欢

转载自www.cnblogs.com/aloneblog/p/9572448.html