Wax: problem subclassing TextBox

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • alex23

    #1

    Wax: problem subclassing TextBox

    Hey everyone,

    I've just started looking at Wax and have hit a problem I can't
    explain. I want an app to respond to every character input into a
    TextBox.

    Here's a simple, working example:

    +++
    from wax import *

    class MainFrame(Verti calFrame):
    def Body(self):
    self.search = TextBox(self)
    self.search.OnC har = self.OnChar
    self.AddCompone nt(self.search, expand='h', border=5)

    def OnChar(self, event):
    print 'OnChar:', event.GetKeyCod e()
    event.Skip()

    app = Application(Mai nFrame)
    app.Run()
    +++

    This displays a TextBox and entering "abcd" results in:

    OnChar: 97
    OnChar: 98
    OnChar: 99
    OnChar: 100

    Rather than defining the OnChar hook on the main frame, though, it
    makes more sense (to me) to be defined on the TextBox itself, so I
    tried subclassing it as follows:

    +++
    class NewTextBox(Text Box):
    def OnChar(self, event):
    print 'on char', event.GetKeyCod e()
    event.Skip()

    class MainFrame(Verti calFrame):
    def Body(self):
    self.search = NewTextBox(self )
    self.AddCompone nt(self.search, expand='h', border=5)
    +++

    With the same input of 'abcd', I get the following:
    on char 97
    on char 97
    on char 98
    on char 98
    on char 99
    on char 99
    on char 100
    on char 100

    As I understand it, event.Skip() should propagate the event up the
    inheritance chain, but I don't see how that would result in
    NewTextBox.OnCh ar being called _twice_ for each input. Stopping the
    event there by removing the event.Skip() does result in only one 'on
    char XX' line for each character, but also stops _all_ OnChar actions
    for the TextBox - such as updating the value and displaying it - and I
    really don't want to have to reimplement the full functionality just to
    prevent this.

    Is there something glaringly obvious that I'm doing wrong in the above
    code?

    Thanks for any help...

    - alex23

  • Hans Nowak

    #2
    Re: Wax: problem subclassing TextBox

    alex23 wrote:
    Hey everyone,
    >
    I've just started looking at Wax and have hit a problem I can't
    explain. I want an app to respond to every character input into a
    TextBox.
    >
    Here's a simple, working example:
    >
    +++
    from wax import *
    >
    class MainFrame(Verti calFrame):
    def Body(self):
    self.search = TextBox(self)
    self.search.OnC har = self.OnChar
    self.AddCompone nt(self.search, expand='h', border=5)
    >
    def OnChar(self, event):
    print 'OnChar:', event.GetKeyCod e()
    event.Skip()
    >
    app = Application(Mai nFrame)
    app.Run()
    +++
    >
    This displays a TextBox and entering "abcd" results in:
    >
    OnChar: 97
    OnChar: 98
    OnChar: 99
    OnChar: 100
    >
    Rather than defining the OnChar hook on the main frame, though, it
    makes more sense (to me) to be defined on the TextBox itself, so I
    tried subclassing it as follows:
    >
    +++
    class NewTextBox(Text Box):
    def OnChar(self, event):
    print 'on char', event.GetKeyCod e()
    event.Skip()
    >
    class MainFrame(Verti calFrame):
    def Body(self):
    self.search = NewTextBox(self )
    self.AddCompone nt(self.search, expand='h', border=5)
    +++
    >
    With the same input of 'abcd', I get the following:
    on char 97
    on char 97
    on char 98
    on char 98
    on char 99
    on char 99
    on char 100
    on char 100
    Heh, that's a bug. As a temporary solution, go to textbox.py and
    comment out the line in the __events__ dict that says 'Char': wx.EVT_CHAR.

    I will need to fix the way Wax handles events like these; this will
    probably be solved in the next release.

    --
    Hans Nowak
    Memimpin Angin Perubahan Teknologi


    Comment

    Working...