【问题标题】:How to synchronize wxpython Gauge widget with threads in python?如何将 wxpython Gauge 小部件与 python 中的线程同步?
【发布时间】:2017-03-17 04:27:43
【问题描述】:

我的 python 项目有多个线程正在运行。我想从 wxpython 库中添加一个仪表小部件来显示进度。我希望仪表充满直到我的第一个线程完成。我如何实现这一目标?我在 Windows 上使用 Python 2.7。

【问题讨论】:

    标签: python-2.7 wxpython progress-bar wxwidgets python-multithreading


    【解决方案1】:

    使用 wx.CallAfter

    def add_to_the_gauge(value):
        your_gauge.Value += value
    
    
    ...
    #in some thread
    def some_thread_running():
        wx.CallAfter(add_to_the_gauge, 2)
    

    【讨论】:

      【解决方案2】:

      您需要将工作线程中的事件发布到主线程,要求其更新仪表,请参阅this overview。

      【讨论】:

        【解决方案3】:

        你可以使用一些简单的东西 记得导入模块:

        import os
        

        然后把它放在框架上

        def __init__(self, *a, **k):
            filename = 'game.exe'
        
        
            self.timer = wx.Timer()
            self.Bind(wx.EVT_TIMER, self.OnUpdateGauge, self.timer)
            self.timer.Start()
            self.proc = os.popen(filename) # start the process
        
        def OnUpdateGauge(self, event):
            if self.proc.poll() == None: # return None if still running and 0 if stopped
                self.timer.Stop()
                return
            else:
                '''do something with the gauge'''
        

        希望得到帮助

        【讨论】:

          猜你喜欢
          • 2011-12-28
          • 1970-01-01
          • 1970-01-01
          • 2013-10-14
          • 2012-02-19
          • 2021-01-31
          • 2012-03-20
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多