如何在Python中同步wxpython Gauge小部件和线程?

问题描述:

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

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

使用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) 

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

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''' 

希望在那些帮助