2011-10-01 56 views
0

我發現wx.CallAfterwx.CallLater在文檔中,但都沒有解決我的問題。 我想要做的是在做任務時更新狀態欄,但wx.CallAfterwx.CallLater只在任務後更新。在wxpython中有一個wx.CallDuring

例子:

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 
# generated by wxGlade 0.6.3 on Fri Sep 30 20:55:34 2011 

import wx 
import time 
# begin wxGlade: extracode 
# end wxGlade 



class MyFrame1(wx.Frame): 
    def __init__(self, *args, **kwds): 
     # begin wxGlade: MyFrame1.__init__ 
     kwds["style"] = wx.DEFAULT_FRAME_STYLE 
     wx.Frame.__init__(self, *args, **kwds) 
     self.frame_2_statusbar = self.CreateStatusBar(1, 0) 

     self.__set_properties() 
     self.__do_layout() 
     # end wxGlade 

    def __set_properties(self): 
     # begin wxGlade: MyFrame1.__set_properties 
     self.SetTitle("frame_2") 
     self.frame_2_statusbar.SetStatusWidths([-1]) 
     # statusbar fields 
     frame_2_statusbar_fields = ["foo"] 
     for i in range(len(frame_2_statusbar_fields)): 
      self.frame_2_statusbar.SetStatusText(frame_2_statusbar_fields[i], i) 
     # end wxGlade 

    def __do_layout(self): 
     # begin wxGlade: MyFrame1.__do_layout 
     self.Layout() 
     # end wxGlade 
    def Test(self): 
    time.sleep(10) 
     for i in range(0,100): 
     time.sleep(0.1) 
     txt="I <3 Stack Exchange x " +str(i) 
     wx.CallAfter(self.frame_2_statusbar.SetStatusText,txt, 0) 
     wx.CallAfter(self.Update) 
     print txt 
# end of class MyFrame1 


if __name__ == "__main__": 
    app = wx.PySimpleApp(0) 
    wx.InitAllImageHandlers() 
    frame_1 = MyFrame1(None, -1, "") 
    app.SetTopWindow(frame_1) 
    frame_1.Show() 
    wx.CallAfter(frame_1.Test) 
    app.MainLoop() 

回答

3

從它的聲音, 「CallDuring」 無非是簡單地調用任何你想要的功能更多。它會在你打電話時被完全調用。爲什麼直接調用它並不能解決您的問題,是否有理由?

但是,你告訴你的程序要睡覺。由於您將整個應用程序設置爲睡眠狀態,因此「過程中」的任何次數都不會幫助您。也許你認爲sleep是一個很好的「真實」代碼模擬,但事實並非如此。你實際上是在告訴你的程序「停止所有處理」,其中包括屏幕更新。

問題的癥結在於你不允許事件循環來處理重繪事件。你可以嘗試在你的循環中調用wx.Yield(),但是在GUI程序的主線程中有一個大的,長時間運行的循環是一種代碼異味。幾乎可以肯定有更好的方式來處理你的問題。

我可以給的最好的建議是搜索「wxpython長時間運行的任務」。最有可能的第一個命中將是一個wxpywiki頁面,標題爲「Long Running Tasks」,您可能會發現有幫助。

+0

wx.Yield(),作品謝謝:) – linuxrules94