Entry validation question


I'm trying to validate the text entered in an entry box so that the user
can only input text that represents a valid floating point number. I'd
been using a SpinBox, and that widget has a set_numeric() method, but
the problem with that method is it makes the widget reject decimal
points, so calling a method that seemed promsing ends up being a trap.

My code below is written in Python, and I'm using PyGTK to do the
interface. I've got the 'insert-text' signal handler working, and the
entry widget will allow floating point values, but the problem I'm
seeing is the entry box cursor is staying put at the beginning of the
entry box. After adding a character it should move to the end, and all
the things I've tried to do this have failed. Calling set_position()
seems to set the widget up correctly so the new character is added at
the end of the string, but the cursor remains stuck. Even the
'move-cursor' signal emission fails.

The sample code from the GTK documentation shows how to intercept the
'insert-text' signal and filter the text the user inputs, and there is
no indication of having to call set_position() or emit() to get the
cursor to move. Even a bit of sample code I found on the Gtk::Perl list
just handles 'insert-text' without any indication of moving the cursor.
So, can someone point out to me what is required in getting the entry
widget cursor to advance to the end of the text string in the widget?

Thanks in advance.

Art Haas


# 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')
        _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)
                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)
        # _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)

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

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

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

if __name__ == '__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

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]