【发布时间】:2014-03-13 03:40:05
【问题描述】:
我已经建立了一个wx.Dialog,其中一些wx.Radiobox 需要验证。如果用户没有做出选择,验证器不会返回True 并弹出wx.MessageBox 告诉用户你应该选择一些东西。同时,背景颜色应该已经变成了粉红色。
这里的问题是在使用RadioBox.SetBackgroundColour("pink")时,RadioBox的标签文本的背景颜色变成了粉红色,这是完全正常的,但是RadioButton的标签文本的背景颜色直到鼠标经过才会改变。
我想知道为什么会这样以及如何解决它。我在官方文档中查找并搜索过,但一无所获。有人知道吗?
平台是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) 是否在所有平台上 2) 在所有平台上 3) 调整可能会解决什么问题。从表面上看,这听起来像是 wx 中的一个错误。
-
@GreenAsJade 感谢关注和提醒,底部添加了一个简单的运行代码,希望对您有所帮助
-
嘿 - 运行它的包装器如此简单,但对于来帮助你的人来说,最好有它,因为弄清楚它可能并非易事:)