wxpython使用 publisher进度条_wxPython使用Publisher实现进度条

1 importtime2 importwx3 from threading importThread4 from wx.lib.pubsub importPublisher5

6 classParseMML():7 souceFile=‘‘

8 targetFile=‘‘

9 progressNum=010

11 def __init__(self):12 pass

13

14 defrun(self):15 for i in range(0,101,10):16 time.sleep(1)17 ParseMML.progressNum=i18 print(i)19

20 ########################################################################

21 classTestThread(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 defrun(self):30 """Run Worker Thread."""

31 #This is the code executing in the new thread.

32 Thread(target=ParseMML().run).start()33 currentPro=034 whileTrue:35 pro =ParseMML.progressNum36 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=pro42 wx.CallAfter(Publisher().sendMessage, "update", 100)43 wx.MessageBox("Done...", "message", wx.OK)44

45 ########################################################################

46 classMyProgressDialog(wx.Dialog):47 """"""

48 #----------------------------------------------------------------------

49 def __init__(self):50 """Constructor"""

51 wx.Dialog.__init__(self, None, title="Progress",size=(250,50))52 self.count =053 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 defupdateProgress(self, msg):62 """

63 Update the progress bar64 """

65 self.count =msg.data66 if self.count >= 100:67 self.Destroy()68

69 self.progress.SetValue(self.count)70

71 ########################################################################

72 classMyFrame(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 defonButton(self, event):85 """

86 Runs the thread87 """

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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值