Hi.

I'm making progress in understanding how to validate the data in an
entry box, but I find myself now at a cursor-related puzzle.

My validation test code is below. A window with an entry box appears,
and the idea is to only accept what will be a valid floating point
number. The bits about '-' and '+' signs aren't used right yet, but
eventually will be. What the code below does do is restrict the valid
characters that the user can enter, then test to see if the text in the
entry box plus the new character can be converted into a float. It works
well enough, but what I'm stumped on is how to move the cursor. The dumb
cursor just sits at the beginning of the entry box. I thought
set_position() would move it, but it does not move. What is the magic
command to make the cursor advance to the end of the string? I'm sure
I'm overlooking something ...

If the stop_emission() call is commented out, then the cursor advances
without problem, but also the validation routine is effectively ignored.
I guess that a default "insert-text" handler is called, and this handler
does the magic to move the cursor.

Thanks in advance for any cursor-moving advice you can send.

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_insert_text(entry, new_text, new_text_length, position):
    print "in entry_insert_text() ..."
    sys.stdout.write("new_text: '%s'\n" % new_text)
    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)
        if _string != '-' and _string != '+':
            try:
                sys.stdout.write("trying string ...\n")
                _val = float(_string)
                _hid = entry.get_data('handlerid')
                entry.set_position(-1)
                _pos = entry.get_position()
                entry.handler_block(_hid)
                _pos = entry.insert_text(new_text, _pos)
                entry.handler_unblock(_hid)
                entry.set_position(_pos)
            except StandardError, e:
                sys.stdout.write("exception: '%s'\n" % e)
    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("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