Howdy,
I'm really confused. I have a TreeCtrl that I am trying to add a simple validator so that the items are never empty.
I have shamelessly stolen the validator code from wxPIA and it never appears to be executed.
To be more precise, when I instantiate the tree, the validator init code is called once, but when
a node on the tree is opened for editing, and then closed, the validate code does not fire. It's as if the tree
has a validator (whatever that means), but the text node does not.
I'm including an absolutely minimal example. Run the example and edit the "root node" label. No validation
is performed. Do I have to attach a validator to each node when I open it for editing???
TIA,
Danny
import wx
class NotEmptyValidator(wx.PyValidator):
def __init__(self):
wx.PyValidator.__init__(self)
print "in validator"
def Clone(self):
print "in Clone, about to create another validator"
return NotEmptyValidator()
def Validate(self, win):
print "in validate"
textCtrl = self.GetWindow()
text = textCtrl.GetValue()
if len(text) == 0:
wx.MessageBox("This field must contain some text!", "Error")
textCtrl.SetBackgroundColour("pink")
textCtrl.SetFocus()
textCtrl.Refresh()
return False
else:
textCtrl.SetBackgroundColour(
wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOW))
textCtrl.Refresh()
return True
def TransferToWindow(self):
print "transfer to"
return True
def TransferFromWindow(self):
print "transfer from"
return True
class MyFrame(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, wx.Size(450, 350))
tree = wx.TreeCtrl(self, 1, wx.DefaultPosition, (-1,-1), wx.TR_HAS_BUTTONS|wx.TR_EDIT_LABELS,validator=NotEmptyValidator())
root = tree.AddRoot('Root Node')
self.Centre()
class MyApp(wx.App):
def OnInit(self):
frame = MyFrame(None, -1, 'treectrl.py')
frame.Show(True)
return True
app = MyApp(0)
app.MainLoop()
treeValidatorProblem.py (1.29 KB)