TAB and Enter key in Tkinter

klappnase klappnase at web.de
Sat Aug 9 10:19:24 CEST 2003


Scott Holmes <scottholmes at sbcglobal.net> wrote in message news:<mailman.1060391355.7000.python-list at python.org>...
> I've noticed that widgets in a Pmw toplevel are navigated by tab key.  I 
> find I can tab between the entry fields and the buttons but hitting the 
> enter key after moving to a button does not activate the button.  If 
> possible, how do I configure buttons to accept and enter key event?
> -- 
> ---------------------------------------------------------------------
Hi,

I suppose you probably bound the callback to your button with the
"command=" option, like:

button1 = Button(parent, text="Hello", command=some_function)

In this case you will get the default Tk behavior that the callback is
invoked with a (mouse-) Button-1 or a Space-Key press.
If you do not want this, use the bind method instead like:

button1 = Button(parent, text="Hello")
button1.bind("<ButtonRelease-1>", some_function)
button1.bind("<KeyRelease-Return>", some_function)

This way you will get the behavior I think you want. Of course you can
bind any other event to your callback this way.
Please note that the "bind" method unlike the "command" option passes
the event as (the first) argument to the callback function, so you
will have to change the definition line like this:

#callback for use with "command" option
def some_function():
    do_something

#callback for use with "bind" method
def some_function(event):
     do_something

I hope this helped.

Best regards

Michael




More information about the Python-list mailing list