wx.Radiobox.SetBackgroundColour("pink") 在鼠标经过前不会更改单选按钮颜色
我创建了一个 wx.Dialog
对话框,里面有一些 wx.Radiobox
需要进行选择验证。如果用户没有做出选择,验证器不会返回 True
,然后会弹出一个 wx.MessageBox
提醒用户必须选择一个选项。同时,背景颜色应该变成粉色。
这里的问题是,当我使用 RadioBox.SetBackgroundColour("pink")
时,RadioBox的标签文字背景颜色变成了粉色,这很正常,但RadioButton的标签文字背景颜色不会改变,直到鼠标经过时才会变。
我在想这是为什么,以及如何解决这个问题。我查阅了官方文档和谷歌搜索,但没有找到相关信息。有没有人知道该怎么做?
我的平台是win8.1x64,使用的python版本是2.7.3,wxpython版本是2.8.12.1。由于使用的是Psychopy独立包,所以这两个版本都比较旧。
对话框的代码如下,这是一个相当简单的例子:
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 个回答
0
这个问题是wxpython2.8.12.1里的一个错误,已经在wxpython3.0.0.0版本中修复了。感谢@GreenAsJade的提醒。