Can someone please point me in the right direction? I have a wxpython
program, and have a few fields where to enter data. I'd like to save that
data, but to do a cursory check that all fields are filled in before
activating the SAVE button. Can someone nudge me in the right direction on
how to check to see if the textctrls have data?
I was looking thru the list of wxpython events, and didn't see one I could
use really. I think that as I leave a text control, I could bind that to a
routine that looks to see if a .GetValue()=="" for each of the 4 text
controls I have, but I don't know what the event would be for changing the
active text control.
Thanks for your help!!!!
Dave
GMane Python wrote:
Can someone please point me in the right direction? I have a wxpython
program, and have a few fields where to enter data. I'd like to save that
data, but to do a cursory check that all fields are filled in before
activating the SAVE button. Can someone nudge me in the right direction on
how to check to see if the textctrls have data?
I was looking thru the list of wxpython events, and didn't see one I could
use really. I think that as I leave a text control, I could bind that to a
routine that looks to see if a .GetValue()=="" for each of the 4 text
controls I have, but I don't know what the event would be for changing the
active text control.
Sounds like you want to bind to wx.EVT_KILLFOCUS. Bind each textctrl's EVT_KILLFOCUS to a common handler, and have that handler maintain the state. When the last text control has been filled in, the handler can call Enable() on the save button.
ยทยทยท
--
pkm ~ http://paulmcnett.com
I'd like to save that
data, but to do a cursory check that all fields are filled in before
activating the SAVE button.
You could try using validators to ensure there is a value in each
control. We use a class like this:
class NotEmptyValidator(Validator):
def __init__(self):
wx.PyValidator.__init__(self)
def Clone(self):
return NotEmptyValidator()
def Validate(self, win):
textCtrl = self.GetWindow()
text = textCtrl.GetValue()
if text == None or len(str(text).strip()) == 0:
textCtrl.SetBackgroundColour('yellow')
textCtrl.SetFocus()
textCtrl.Refresh()
return False
else:
textCtrl.SetBackgroundColour(
wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOW))
textCtrl.Refresh()
return True
To get the enabled/disabled save button behaviour, you could bind to the
EVT_KILL_FOCUS event for each control:
class MyDialog(wx.Dialog):
def __init__(self, *a, **k):
wx.Dialog.__init__(self, *a, **k)
text1 = wx.TextCtrl(self, 0, validator=NotEmptyValidator())
text2 = wx.TextCtrl(self, 0, validator=NotEmptyValidator())
text1.Bind(EVT_KILL_FOCUS, self.lostFocus)
text2.Bind(EVT_KILL_FOCUS, self.lostFocus)
def lostFocus(self, evt):
evt.Skip()
if self.Validate():
self.saveButton.Enable()
This has the added advantage of re-validating the controls before the
user presses "Save", which could be complex to do in
Hope this helps.
Peter