2014-06-22 29 views
0

我遇到的問題是,當我在面板中設置靜態文本(請參閱#Case 2)時,它只呈現第一個字母。下面的代碼是一個超級精簡版的我實際的代碼,但它會產生相同的結果:wxPython面板文本不呈現

import wx 
import time 

class TestInterface(wx.Frame): 

    testStatusFlag = 0 

    def __init__(self, *args, **kw): 
     super(TestInterface, self).__init__(*args, **kw) 

     self.pnl = wx.Panel(self) 

     self.SetSize((450, 225)) 
     self.SetTitle('example') 
     self.Centre() 
     self.Show(True) 

     self.indicatorFullTest = None 
     self.buttonFullTest = None 
     self.setTestStatus(status=1) 


    def runTest(self, ev=None): 

     self.setTestStatus(status=2) 

     #the test is a bunch of functions that take a bunch of time to run 
     #they're all located in separate files but all access a single piece of hardware 
     #so multithreading is effectively impossible (I don't want to spend days re-writing stuff to accomodate it) 
     time.sleep(10) 

     self.setTestStatus(status=3) 
     return 0 


    def setTestStatus(self, ev=None, status=None): 

     #Handle the optional status argument 
     if (status in [1,2,3]): 
      self.testStatusFlag = status 

     #Remove any old stuff since if we're calling this function they have to get removed 
     if (self.indicatorFullTest != None): 
      self.indicatorFullTest.Hide() 
     if (self.buttonFullTest != None): 
      self.buttonFullTest.Hide() 

     #Case 1 
     if (self.testStatusFlag == 1): 
      self.buttonFullTest = wx.Button(self.pnl, label='Run Test', pos=(125, 100), size=(250, 50)) 
      self.buttonFullTest.Bind(wx.EVT_BUTTON, self.runTest) 

     #Case 2 
     elif (self.testStatusFlag == 2): 
      self.indicatorFullTest = wx.Panel(self.pnl, pos=(125, 100), size=(250, 50)) 
      wx.StaticText(self.indicatorFullTest, wx.ID_ANY, "Full-Board Test now in progress\nAllow up to 6 min to finish...", 
                style=wx.ALIGN_CENTRE_HORIZONTAL, pos=(18,7)) 
      self.indicatorFullTest.SetBackgroundColour('Tan') 
      self.Update() 

     #Case 3 
     elif (self.testStatusFlag == 3): 
      self.buttonFullTest = wx.Button(self.pnl, label='Test Complete\nPress to reset GUI', 
              pos=(125, 100), size=(250, 50)) 
      self.buttonFullTest.SetBackgroundColour((130,255,130)) 
      self.buttonFullTest.Bind(wx.EVT_BUTTON, self.resetGUI) 


    #Resets the GUI after a test is complete 
    def resetGUI(self, ev=None): 
     self.setTestStatus(status=1) #Reset the fullTest button/indicator thing 



if __name__ == '__main__': 
    ex = wx.App() 
    gui = TestInterface(None) 
    ex.MainLoop() 

基本上,我怎麼做UI完全呈現的文本?我想這與改變指標後不回wx的主循環有關,但我覺得自己調用self.Update()應該是不必要的。這也可能與我在使用按鈕和使用面板之間切換的方式有關(可能很糟糕,但我不知道該如何做)。我知道我可以通過讓我的測試函數在一個單獨的線程中運行來解決這個問題,但問題是測試函數會調用單獨的庫,我實際上沒有時間重寫。

感謝

回答

0

這是一個更容易一點,當你的wxPython使用的分級機,因爲這樣你可以顯示和隱藏部件。你可以在這個tutorial中看到一個很好的例子。我會推薦學習sizer,以便在調整框架大小時使您的UI更具動感。

但無論如何,這個難題的答案非常簡單。而不是在案例#2中調用self.Update(),則需要調用wx.Yield()。我換了一個換行,它在Linux上適用於我。