validator in TreeCtrl?

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)

I should mention, that I don't expect the Validate code to work as written. It's unchanged from the book, the
problem is that it doesn't fire at all.

D

···

At 03:39 PM 1/16/2007 -0700, you wrote:

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()

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

Hi Danny,

    you might also try another approach, using the edit event of
wx.TreeCtrl (remember to use the wx.TR_EDIT_LABEL style in your
constructor):

tree.Bind(wx.EVT_TREE_BEGIN_LABEL_EDIT, self.OnBeginEdit)
tree.Bind(wx.EVT_TREE_END_LABEL_EDIT, self.OnEndEdit)

    def OnBeginEdit(self, event):

        # show how to prevent edit...
        item = event.GetItem()
        if item and self.tree.GetItemText(item) == "The Root Item":
            wx.Bell()
            event.Veto()

    def OnEndEdit(self, event):

        # show how to reject edit, we'll not allow empty item texts
        label = event.GetLabel()
        if label.strip() == "":
                event.Veto()
                return

HTH.

Andrea.

···

On 1/16/07, Danny Shevitz <shevitz@lanl.gov> wrote:

I should mention, that I don't expect the Validate code to work as written.
It's unchanged from the book, the
problem is that it doesn't fire at all.

D

At 03:39 PM 1/16/2007 -0700, you wrote:
>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()
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
>For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

--
Andrea.

"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.virgilio.it/infinity77/

Danny Shevitz wrote:

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've never understood why things like the TreeCtrl and ListCtrl have validators, since they use a separate widget for the input. So in essence your statement above is true, and you'll want to use something other than the validator to manage the input in the editable nodes, such as what Andrea suggested.

The validator could be used however for data transfer and validation of the whole tree, if it is on a dialog or you call Validate or the Transfer functions on the panel.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!