I'm still working the entry box validation, and for now I'm trying to
stick with the approach of intercepting the insert-text signal. I've
added a call where the entry box emits a 'move-cursor' signal, and still
no luck. The sample C code in the GTK documentation doesn't indicate
that anything more than handling the 'insert-text' signal is needed, and
some of the examples I've found from Google also do nothing more than
handle 'insert-text'. Can anyone point out just what I'm missing to make
the entry box cursor move to the end of the text?

The code below now accepts the '-' and '+' characters. If they are the
only characters in the entry when a 'focus-out' signal is handled then
the entry box is cleared. I'm still not sure of a good way to test for
these characters yet, but at least now negative numbers can be added in
the entry box.

Art Haas
=================================================
#!/usr/bin/python

#
# entry box validation test
#

import gtk

import sys

def entry_activate(entry):
    print "in entry_activate() ..."
    _text = entry.get_chars(0, -1)
    sys.stdout.write("text: '%s'\n" % _text)
    entry.delete_text(0, -1)

def entry_changed(entry):
    print "in entry_changed() ..."
    _text = entry.get_chars(0, -1)
    sys.stdout.write("text: '%s'\n" % _text)

def entry_key_press(entry, event):
    print "in entry_key_press() ..."
    return gtk.FALSE

def entry_key_release(entry, event):
    print "in entry_key_release() ..."
    return gtk.FALSE

def entry_focus_in(entry, event):
    print "in entry_focus_in() ..."
    return gtk.FALSE

def entry_focus_out(entry, event):
    print "in entry_focus_out() ..."
    _text = entry.get_chars(0, -1)
    if _text == '-' or _text == '+':
        entry.delete_text(0, -1)
    return gtk.FALSE

def entry_move_cursor(entry, step, arg1, arg2):
    print "in entry_move_cursor() ..."
    print "step: %d" % step
    print "count: %d" % arg1
    print "ext. sel: %d" % arg2
    
def entry_insert_text(entry, new_text, new_text_length, position):
    print "in entry_insert_text() ..."
    sys.stdout.write("new_text: '%s'\n" % new_text)
    print "position: " + `position`
    if (new_text.isdigit() or
        new_text == '.' or
        new_text == '+' or
        new_text == '-'):
        sys.stdout.write("good character: '%s'\n" % new_text)
        _string = entry.get_chars(0, -1) + new_text
        sys.stdout.write("string: '%s'\n" % _string)
        _hid = entry.get_data('handlerid')
        entry.handler_block(_hid)
        entry.set_position(-1)
        _pos = entry.get_position()
        print "pos: %d" % _pos
        # _pcp = entry.get_property("cursor-position")
        # print "property cursor position: %d" % _pcp
        _move = True
        if _string == '-' or _string == '+':
            _pos = entry.insert_text(new_text, _pos)
        else:
            try:
                sys.stdout.write("trying string ...\n")
                _val = float(_string)
                _pos = entry.insert_text(new_text, _pos)
                print "pos: %d" % _pos
            except StandardError, e:
                _move = False
                sys.stdout.write("exception: '%s'\n" % e)
        entry.handler_unblock(_hid)
        # _pcp = entry.get_property("cursor-position")
        # print "property cursor position: %d" % _pcp
        # if _move:
            # entry.emit("move_cursor", gtk.MOVEMENT_VISUAL_POSITIONS,
                       # len(new_text), gtk.FALSE)
        entry.set_position(-1)
    entry.stop_emission("insert-text")

def main():
    _window = gtk.Window()
    _window.set_title("Entry box validation.")
    _window.set_border_width(10)
    _window.connect('destroy', lambda win: gtk.main_quit())

    _hbox = gtk.HBox(gtk.FALSE, 10)
    _window.add(_hbox)

    _label = gtk.Label("Enter a number:")
    _hbox.pack_start(_label, gtk.FALSE, gtk.FALSE, 0)

    _entry = gtk.Entry()
    _entry.connect("activate", entry_activate)
    _entry.connect("move_cursor", entry_move_cursor)
    # _entry.connect("key_press_event", entry_key_press)
    # _entry.connect("key_release_event", entry_key_release)
    # _entry.connect("focus_in_event", entry_focus_in)
    _entry.connect("focus_out_event", entry_focus_out)
    # _entry.connect("changed", entry_changed)
    _handlerid = _entry.connect("insert-text", entry_insert_text)
    _entry.set_data('handlerid', _handlerid)
    print "handlerid: %d" % _handlerid
    _hbox.pack_start(_entry, gtk.TRUE, gtk.TRUE, 0)
    
    _window.show_all()
    gtk.main()

if __name__ == '__main__':
    main()
====================================================
-- 
Man once surrendering his reason, has no remaining guard against absurdities
the most monstrous, and like a ship without rudder, is the sport of every wind.

-Thomas Jefferson to James Smith, 1822
_______________________________________________
pygtk mailing list   [EMAIL PROTECTED]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/

Reply via email to