簡體   English   中英

wx.Gauge不實時更新

[英]wx.Gauge not updating in real-time

我無法實時更新wx.Gauage。 wx.Gauge在名為ProgressWindow的類中實例化。 ProgressWindow用於在工作完成時提供進度條。 更新量規是在單獨的線程上完成的,因此它不會阻止正在進行的工作。 不幸的是,當我實例化並啟動進度窗口時,量規僅在“ 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中的內容時將事件從該事件發布到主線程。 然后,主線程不需要做任何特殊的事情,除了為這些事件定義處理程序以更新進度指示器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM