Mike Driscoll wrote:
Hello
I'm writing a simple app to take some details from users at login.
So far I have setup validation to ensure that the various text fields
are filled in and to collect the input from these fields.
However, in one case the text field should only contain some text if a
checkbox has been checked. In other words, the validator for this
text field only needs to run if the checkbox has been checked.
Clearly the validation can't take place immediately the checkbox is
clicked as then the user won't have any time to fill the text field.
I would be grateful for any suggestions as this is my first wxPython project.
Adam
If I were you, I would set the text control to disabled until the checkbox is checked. Then in the checked event handler, I would enable the text control. Text controls accept validators as part of their instantiation, so if you create it with one already linked, you won't need to add it later. I'm not even sure you can add one after it's been created. Anyway, the validator doesn't do anything until the user actually starts typing into the text control.
Thanks for the advice. I've tried to do that, but a validator is being called as soon as the text control is enabled. I've based my code on the wxPIA examples that came with my installation, in the Chapter-09 directory. Here's the relevant code:
class MyDialog(wx.Dialog):
def __init__(self, data):
wx.Dialog.__init__(self, None, wx.ID_ANY, 'Validation test', pos=(100, 100),
size=(1000, 700))
text = wx.StaticText(self, -1, "User name and account details",
(20, 10))
text.SetFont(wx.Font(14, wx.SWISS, wx.NORMAL, wx.BOLD))
self.text2 = wx.TextCtrl(self, -1, "Test text", size=(175, -1),
pos=(20, 150), validator=DataXferValidator(data, "account"))
self.acctick_c = wx.CheckBox(self, -1, "Tick if you don't know your account details",
(20, 330))
self.acctick_l = wx.StaticText(self, -1, "If ticked, please enter your e-mail address:",
pos=(20, 380))
self.acctick_t = wx.TextCtrl(self, -1, "", size=(175, -1),
pos=(330, 380), validator=DataXferValidator(data, "email") )
self.acctick_t.Disable()
self.acctick_l.Disable()
self.acctick_c.Bind(wx.EVT_CHECKBOX, self.ShowTextCtrl)
self.okay = wx.Button(self, wx.ID_OK, pos=(500, 500))
self.okay.SetDefault()
def ShowTextCtrl(self, event):
self.acctick_t.Enable()
self.acctick_l.Enable()
At the moment the validation works as expected for the first text field. For the second text filed, which is disabled until the box is checked, the validation is called anyway, even though the user can't edit it.
Do I need to add another test in the validation code, to ensure the window has been enabled?
Adam
···
See the following for more on Validators:
Validator for Object Attributes - wxPyWiki
DataFormatters - wxPyWiki
There's also a Validator demo in the wxPython demo.
Mike
_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users