wx.Gauge 没有实时更新
wx.Gauge not updating in real-time
我无法实时更新 wx.Gauage。 wx.Gauge 在名为 ProgressWindow 的 class 中实例化。 ProgressWindow 用于在工作完成时提供进度条。
更新仪表是在一个单独的线程上完成的,这样它就不会阻塞正在完成的工作。不幸的是,当我实例化并启动进度 window 时,仪表仅在 "fn_testing_progress_window" 测试函数的最后更新。任何人都可以看到为什么会这样吗?我正在尝试在调用 "fn_increment_count" 时更新仪表。
注意:我之所以有一个队列来处理更新量表的请求,是因为如果正在完成的工作分布在多个线程中,则每个线程都可以更新量表。
def fn_testing_progress_window():
pw = ProgressWindow(self.main_panel)
pw.fn_start()
pw.fn_increment_count(increment = 50, msg = "50 Now")
time.sleep(3)
pw.fn_increment_count(increment = 75, msg = "75 Now")
time.sleep(3)
pw.fn_increment_count(increment = 100, msg = "Finished")
def fn_start(self):
print "Starting Progress"
_runner_thread = threading.Thread(target = self.fn_run)
_runner_thread.start()
def fn_run(self):
self._running = True
self._current_count = 0
while(self._running):
# Wait til there is something in queue or til run is stopped
while True:
if (not self._update_queue.empty()):
print "not empty"
break
if (not self._running):
break
_value, _msg = self._update_queue.get()
# Update progress bar accordingly
if _value:
self._current_count += _value
print "Updating value: %d" %(self._current_count)
wx.CallAfter(self._progress_gauge.SetValue, self._current_count)
if _msg:
print "Updating msg: %s" %(_msg)
wx.CallAfter(self._progress_output.SetValue, str(_msg))
if (self._current_count >= self._total_count):
pass
# Have time for msg to appear
time.sleep(0.1)
def fn_increment_count(self,
increment = 1,
msg = None):
"""
Ability to update current count on progress bar and progress msg.
If nothing provided increments current count by 1
"""
_item_l = (increment, msg)
self._update_queue.put(_item_l)
您不能从单独的线程更新 wxGauge
或任何其他 GUI 控件。 GUI 更新应该只从主 GUI 线程完成,所以你的主线程不能阻塞(就像你做的那样):这不仅可以防止更新发生,而且还会使你的整个程序无响应。
相反,以相反的方式进行:在另一个线程中执行您的长时间工作,并且只要需要在 GUI 中更新某些内容,就从它向主线程发送 post 事件。然后主线程不需要做任何特殊的事情,除了为这些事件定义处理程序来更新进度指示器。
我无法实时更新 wx.Gauage。 wx.Gauge 在名为 ProgressWindow 的 class 中实例化。 ProgressWindow 用于在工作完成时提供进度条。 更新仪表是在一个单独的线程上完成的,这样它就不会阻塞正在完成的工作。不幸的是,当我实例化并启动进度 window 时,仪表仅在 "fn_testing_progress_window" 测试函数的最后更新。任何人都可以看到为什么会这样吗?我正在尝试在调用 "fn_increment_count" 时更新仪表。
注意:我之所以有一个队列来处理更新量表的请求,是因为如果正在完成的工作分布在多个线程中,则每个线程都可以更新量表。
def fn_testing_progress_window():
pw = ProgressWindow(self.main_panel)
pw.fn_start()
pw.fn_increment_count(increment = 50, msg = "50 Now")
time.sleep(3)
pw.fn_increment_count(increment = 75, msg = "75 Now")
time.sleep(3)
pw.fn_increment_count(increment = 100, msg = "Finished")
def fn_start(self):
print "Starting Progress"
_runner_thread = threading.Thread(target = self.fn_run)
_runner_thread.start()
def fn_run(self):
self._running = True
self._current_count = 0
while(self._running):
# Wait til there is something in queue or til run is stopped
while True:
if (not self._update_queue.empty()):
print "not empty"
break
if (not self._running):
break
_value, _msg = self._update_queue.get()
# Update progress bar accordingly
if _value:
self._current_count += _value
print "Updating value: %d" %(self._current_count)
wx.CallAfter(self._progress_gauge.SetValue, self._current_count)
if _msg:
print "Updating msg: %s" %(_msg)
wx.CallAfter(self._progress_output.SetValue, str(_msg))
if (self._current_count >= self._total_count):
pass
# Have time for msg to appear
time.sleep(0.1)
def fn_increment_count(self,
increment = 1,
msg = None):
"""
Ability to update current count on progress bar and progress msg.
If nothing provided increments current count by 1
"""
_item_l = (increment, msg)
self._update_queue.put(_item_l)
您不能从单独的线程更新 wxGauge
或任何其他 GUI 控件。 GUI 更新应该只从主 GUI 线程完成,所以你的主线程不能阻塞(就像你做的那样):这不仅可以防止更新发生,而且还会使你的整个程序无响应。
相反,以相反的方式进行:在另一个线程中执行您的长时间工作,并且只要需要在 GUI 中更新某些内容,就从它向主线程发送 post 事件。然后主线程不需要做任何特殊的事情,除了为这些事件定义处理程序来更新进度指示器。