wxPython进度条

我看到有人迷惑该如何创建一个进度度并更新它。所以我决定做了一不例从一个Thread中更新进度条。这篇批导中,我会创建一个有一个按钮的面版。但按钮被点击后,就会加载一个拥有进度条的对话框并启动一个线程。这是个虚拟线程,除了在20秒内每秒反馈一次更新以外什么都不做,然后这个对话框就销毁了。让我们去试试:


   
   
  1. import time
  2. import wx
  3.  
  4. from threading import Thread
  5.  
  6. from wx. lib. pubsub import Publisher
  7.  
  8. ########################################################################
  9. class TestThread (Thread ):
  10. """Test Worker Thread Class."""
  11.  
  12. #----------------------------------------------------------------------
  13. def __init__ ( self ):
  14. """Init Worker Thread Class."""
  15. Thread. __init__ ( self )
  16. self. start ( ) # start the thread
  17.  
  18. #----------------------------------------------------------------------
  19. def run ( self ):
  20. """Run Worker Thread."""
  21. # This is the code executing in the new thread.
  22. for i in range ( 20 ):
  23. time. sleep ( 1 )
  24. wx. CallAfter (Publisher ( ). sendMessage , "update" , "" )
  25.  
  26. ########################################################################
  27. class MyProgressDialog (wx. Dialog ):
  28. """"""
  29.  
  30. #----------------------------------------------------------------------
  31. def __init__ ( self ):
  32. """Constructor"""
  33. wx. Dialog. __init__ ( self , None , title = "Progress" )
  34. self. count = 0
  35.  
  36. self. progress = wx. Gauge ( self , range = 20 )
  37.  
  38. sizer = wx. BoxSizer (wx. VERTICAL )
  39. sizer. Add ( self. progress , 0 , wx. EXPAND )
  40. self. SetSizer (sizer )
  41.  
  42. # create a pubsub listener
  43. Publisher ( ). subscribe ( self. updateProgress , "update" )
  44.  
  45. #----------------------------------------------------------------------
  46. def updateProgress ( self , msg ):
  47. """
  48. Update the progress bar
  49. """
  50. self. count + = 1
  51.  
  52. if self. count >= 20:
  53. self. Destroy ( )
  54.  
  55. self. progress. SetValue ( self. count )
  56.  
  57.  
  58. ########################################################################
  59. class MyFrame (wx. Frame ):
  60.  
  61. #----------------------------------------------------------------------
  62. def __init__ ( self ):
  63. wx. Frame. __init__ ( self , None , title = "Progress Bar Tutorial" )
  64.  
  65. # Add a panel so it looks the correct on all platforms
  66. panel = wx. Panel ( self , wx. ID_ANY )
  67. self. btn = btn = wx. Button (panel , label = "Start Thread" )
  68. btn. Bind (wx. EVT_BUTTON , self. onButton )
  69.  
  70. sizer = wx. BoxSizer (wx. VERTICAL )
  71. sizer. Add (btn , 0 , wx. ALL|wx. CENTER , 5 )
  72. panel. SetSizer (sizer )
  73.  
  74. #----------------------------------------------------------------------
  75. def onButton ( self , event ):
  76. """
  77. Runs the thread
  78. """
  79. btn = event. GetEventObject ( )
  80. btn. Disable ( )
  81.  
  82. TestThread ( )
  83. dlg = MyProgressDialog ( )
  84. dlg. ShowModal ( )
  85.  
  86. btn. Enable ( )
  87.  
  88. #----------------------------------------------------------------------
  89. # Run the program
  90. if __name__ == "__main__":
  91. app = wx. App ( False )
  92. frame = MyFrame ( )
  93. frame. Show ( )
  94. app. MainLoop ( )
让我们花几钟分解下过程。首先运行 MyFrame 类,一旦你运行脚本你会看到这个:

正如你所见,这见代码只是创建了个有一个按钮的面版。如果你按下按钮你会看到建议了下面的对话框并启动了一新的线程:

让我们来看看创建对话框部分的代码:

   
   
  1. ########################################################################
  2. class MyProgressDialog (wx. Dialog ):
  3. """"""
  4.  
  5. #----------------------------------------------------------------------
  6. def __init__ ( self ):
  7. """Constructor"""
  8. wx. Dialog. __init__ ( self , None , title = "Progress" )
  9. self. count = 0
  10.  
  11. self. progress = wx. Gauge ( self , range = 20 )
  12.  
  13. sizer = wx. BoxSizer (wx. VERTICAL )
  14. sizer. Add ( self. progress , 0 , wx. EXPAND )
  15. self. SetSizer (sizer )
  16.  
  17. # create a pubsub listener
  18. Publisher ( ). subscribe ( self. updateProgress , "update" )
  19.  
  20. #----------------------------------------------------------------------
  21. def updateProgress ( self , msg ):
  22. """
  23. Update the progress bar
  24. """
  25. self. count + = 1
  26.  
  27. if self. count >= 20:
  28. self. Destroy ( )
  29.  
  30. self. progress. SetValue ( self. count )
这段代码只是用 wx.Gauge 部件创建一个对话框。这个 Gauge 只是进度条背后的实际部件。然后,我们在对话框的 __init__ 末尾加入了发布器的监听,这监听会接受来自 updateProgress 方法中触发事件的消息。我们会在线程中看到这个消息。在更新进度条方法中,我们增加计数并通过设置 wx.Gauge 的值去更新它。我们同样要检查计数器是不是小于20,如果达到了就销毁这个对话框。

现在我们来看线程中的代码:


   
   
  1. ########################################################################
  2. class TestThread (Thread ):
  3. """Test Worker Thread Class."""
  4.  
  5. #----------------------------------------------------------------------
  6. def __init__ ( self ):
  7. """Init Worker Thread Class."""
  8. Thread. __init__ ( self )
  9. self. start ( ) # start the thread
  10.  
  11. #----------------------------------------------------------------------
  12. def run ( self ):
  13. """Run Worker Thread."""
  14. # This is the code executing in the new thread.
  15. for i in range ( 20 ):
  16. time. sleep ( 1 )
  17. wx. CallAfter (Publisher ( ). sendMessage , "update" , "" )
这里我们创建一个线程并立马执行,线程中循环20次并利用 time 模块在每次的循环暂停一秒。在每次的暂停之后,它都会发一个消息以告诉对话框更新进度条。

总结

这里你要知道在一个线程中如何创建你自己的进度条对话框。你可以修改这段代码,并用之创建一个文件下载器。如果你真的这样做了,你就得检查下载文件的大小和下载块,所以你可以在适当的范围内创建wx.Gauge并在下载过程中更新它。我希望这可以对你的项目会有些帮助。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值