我用wxPtyhon编写了一个带有两个组合框的对话框,我在其中附加了一个自定义验证器,目的是确保如果输入的值是一个数字字符串。问题是,验证器没有被调用。我做错什么了?在
import wx
# my custom validator class
class NumericObjectValidator(wx.Validator):
def __init__(self):
wx.Validator.__init__(self)
def Clone(self):
return NumericObjectValidator()
# Why isn't this method being called when the user types in the CB field?
def Validate(self, win):
cbCtrl = self.GetWindow()
text = cbCtrl.GetValue()
print 'control value=',text
return True
class SizeDialog(wx.Dialog):
def __init__(self, parent):
wx.Dialog.__init__(self, parent, -1, 'Select Size', size=(200,135))
panel = wx.Panel(self, -1, size=self.GetClientSize())
sizes = map(str, range(3,21))
wx.StaticText(panel, -1, 'Rows:', pos=(10, 15))
self.rows = wx.ComboBox(panel, -1, value='8', choices=sizes,
style=wx.CB_DROPDOWN, pos=(80,15), validator=NumericObjectValidator())
wx.StaticText(panel, -1, 'Columns:', pos=(10,40))
self.cols = wx.ComboBox(panel, -1, choices=sizes, style=wx.CB_DROPDOWN,
pos=(80,40), value='8', validator=NumericObjectValidator())
cancel = wx.Button(panel,wx.ID_CANCEL,'Cancel', pos=(20,75))
wx.Button(panel,wx.ID_OK,'OK', pos=(100,75)).SetDefault()
def TransferToWindow(self):
return True
def TransferFromWindow(self):
return True
def get_size(self):
r = int(self.rows.GetValue())
c = int(self.cols.GetValue())
return (c,r)
if __name__ == "__main__":
app = wx.App(0)
dlg = SizeDialog(None)
dlg.ShowModal()
dlg.Destroy()
我不知道为什么,但是复制wxPython demo就可以了。在
PyValidator
代替Validator
ComboBox
和Button
的父级应该是Dialog
,而不是{以下版本的代码可以正常工作:
这是wxWidgets中的一个怪癖。如果父级不是(的子类)wx.对话框,必须在上调用方法“TransferDataToWindow”和“TransferDataFromWindow”wx.窗口手动操作。这是你用的wx.面板作为组合框的父级,因此不会自动调用数据传输。在
相关问题 更多 >
编程相关推荐