[pygtk] Using changed event

Tony Nelson tonynelson at georgeanelson.com
Tue Jan 31 02:47:12 WST 2006

At 4:24 PM +0100 1/30/06, Nicholas Wieland wrote:
>Hi *,
>I need to trap all non-digits in a gtk.Entry, to prevent hte user
>from inserting letters and symbols at all.
>I'm trying to use the 'changed' event, but it doesn't seem to work:
>   def foo (self, widget):
>     if re.match ('[A-Za-z,.\-+]', widget.get_text () [-1]):
>       return True
>Any idea ?

When you get this signal, the contents have already changed, and any
undesired characters are already there, though probably not yet displayed.

Your code only checks the last character, but the insertion point may not
be at the end, and it would only check one character when pasting even at
the end.  It also seems to assume ASCII.  A better RE might be "\D" (match

This untested code might work for keys and pasting:

    def foo(self, widget):
        s, n = re.subn("\D+", "", widget.get_text())
        if n > 0:

I expect this will result in another "changed" signal.
TonyN.:'                       <mailto:tonynelson at georgeanelson.com>
      '                              <http://www.georgeanelson.com/>

More information about the pygtk mailing list