TAB and Enter key in Tkinter

S

Scott Holmes

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?
--
---------------------------------------------------------------------
Scott Holmes http://sholmes.ws
http://pages.sbcglobal.net/scottholmes
(e-mail address removed)

Independent Programmer/Analyst Passport 4GL
PHP HTML Composer PostgreSQL Informix 4GL, SQL
 
K

klappnase

Scott Holmes said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,526
Members
44,997
Latest member
mileyka

Latest Threads

Top