2014-03-13 329 views
1

我已經構建了wx.Dialog,其中一些wx.Radiobox需要驗證。如果用戶沒有做出選擇,驗證器不會返回True並彈出一個wx.MessageBox告訴用戶您應該選擇一些東西。同時,背景顏色應該變成粉紅色。使用RadioBox.SetBackgroundColour("pink")wx.Radiobox.SetBackgroundColour(「粉色」)不會改變RadioButton的顏色,直到鼠標經過

的這裏的問題是,RadioBox的標籤文本的背景顏色變爲粉紅色,這是完全正常的,但對於單選按鈕的標籤文字的背景色將不會改變,直到鼠標擦肩而過。

我想知道爲什麼這個以及如何解決它。我在官方文檔中查了一下並搜索了一下,但什麼都沒發現。任何人有想法?

該平臺是win8.1x64和python 2.7.3和wxpython 2.8.12.1。對於使用Psychopy獨立軟件包,python和wxpython的版本都很舊。

的對話框如下,一個很簡單的一個:

class Dlg(wx.Dialog): 
    """A simple dialogue box.""" 

    def __init__(self): 
     global app 
     app = wx.PySimpleApp() 
     wx.Dialog.__init__(self, None,-1) 

     self.sizer = wx.FlexGridSizer(cols=1) 

    def show(self, cancelBTN=False): 
     """Show a dialog with 2 RadioBox""" 

     # add two RadioBox 

     RadioBox1 = wx.RadioBox(self, -1, label="Wierd color", choices=["", "choice A", "choice B"], validator=RadioObjectValidator()) 
     RadioBox2 = wx.RadioBox(self, -1, label="Wierd color", choices=["", "choice A", "choice B"], validator=RadioObjectValidator()) 
     RadioBox1.ShowItem(0, show=False) # Hide the first choice 
     RadioBox2.ShowItem(0, show=False) # Hide the first choice 

     RadioBox1.GetValue = RadioBox1.GetStringSelection 
     RadioBox2.GetValue = RadioBox2.GetStringSelection 
     self.sizer.Add(RadioBox1, 1, wx.ALIGN_LEFT) 
     self.sizer.Add(RadioBox2, 1, wx.ALIGN_LEFT) 

     # add buttons for OK 

     self.sizer.Add(wx.Button(self, wx.ID_OK), 1, flag=wx.ALIGN_RIGHT) 

     self.SetSizerAndFit(self.sizer) 

     if self.ShowModal() == wx.ID_OK: 
      pass 
      # do something 

     self.Destroy() 

和校驗,因爲這:

class RadioObjectValidator(wx.PyValidator): 
    """ This validator is used to ensure that the user has entered something 
     into the text object editor dialog's text field. 
    """ 
    def __init__(self): 
     """ Standard constructor. 
     """ 
     wx.PyValidator.__init__(self) 

    def Clone(self): 
     """ Standard cloner. 

      Note that every validator must implement the Clone() method. 
     """ 
     return RadioObjectValidator() 

    def Validate(self, win): 
     """ Validate the contents of the given RadioBox control. 
     """ 
     RadioBox = self.GetWindow() 
     choice = RadioBox.GetValue() 

     if not choice: 
      wx.MessageBox(u'Choose something please', u'info', wx.OK | wx.ICON_EXCLAMATION) 
      RadioBox.SetBackgroundColour("pink") 
      RadioBox.SetFocus() 
      RadioBox.Refresh() 
      return False 
     else: 
      RadioBox.SetBackgroundColour(
       wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOW)) 
      RadioBox.Refresh() 
      return True 

    def TransferToWindow(self): 
     """ Transfer data from validator to window. 

      The default implementation returns False, indicating that an error 
      occurred. We simply return True, as we don't do any data transfer. 
     """ 
     return True # Prevent wxDialog from complaining. 

    def TransferFromWindow(self): 
     """ Transfer data from window to validator. 

      The default implementation returns False, indicating that an error 
      occurred. We simply return True, as we don't do any data transfer. 
     """ 
     return True # Prevent wxDialog from complaining. 

,只需像這樣運行:

if __name__ == "__main__": 
    test = Dlg() 
    test.show() 
+1

你可以提供一個簡單的「主」包裝,所以我們可以運行你的代碼並檢查是否發生1)在所有平臺上2)在所有平臺上3)什麼樣的調整可以修復。在面值時,它聽起來像是wx中的一個bug。 – GreenAsJade

+0

@GreenAsJade感謝您的關注和提醒,在底部添加一個簡單的代碼運行它,並希望這可以幫助 – OrangeCube

+0

Heh - 這樣一個簡單的包裝來運行它,但對於一個人來幫助你,最好是有,因爲它可以是非平凡的弄明白:) – GreenAsJade

回答

0

嗯,這是一個在wxpython2.8.12.1中有bug,並且已經在wxpython3.0.0.0中修復,謝謝@GreenAsJade提醒。