By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,713 Members | 764 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,713 IT Pros & Developers. It's quick & easy.

tkinter button widget

P: n/a
I've got a script where a button gets pushed over and over: to cut down on the
carpal tunnel syndrome I'd like to have the button respond to presses of the
Enter key as well as mouse clicks; can somebody clue me in regarding how this is
done? Muchas gracias.

Peace
Jul 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
You'll have to arrange for the widget with keyboard focus to have a
binding for the "<Return>" event ("<Enter>" is a valid event name, but
it refers to the event generated when the mouse pointer enters a
widget). The called function would call the invoke() method on the
button.

You can create a binding on all widgets within a given toplevel by
making the binding on the toplevel itself.

Example:

import Tkinter

def c():
print "button invoked"

t = Tkinter.Tk()
b = Tkinter.Button(t, text="Do the thing", command=c)
t.bind("<Return>", lambda event: b.invoke())
e = Tkinter.Entry()
e.pack()
b.pack(anchor=Tkinter.E)
t.mainloop()

Jeff

Jul 18 '05 #2

P: n/a
PS To indicate to the user that hitting the Enter key will invoke a particular
button, create the widget with default="active".

Jeff

Jul 18 '05 #3

P: n/a
"Elaine Jackson" <el***************@home.com> wrote in message news:<Ygxpc.492595$Ig.75690@pd7tw2no>...
I've got a script where a button gets pushed over and over: to cut down on the
carpal tunnel syndrome I'd like to have the button respond to presses of the
Enter key as well as mouse clicks; can somebody clue me in regarding how this is
done? Muchas gracias.

Peace


b = Button(master, command=do_something)
b.bind('<Return>', lambda event, key='<space>' : b.event_generate(key))

I hope this helped

Michael
Jul 18 '05 #4

P: n/a
Thanks for replying, but what you suggest doesn't seem to be working. Nothing I
try gets the button to have focus in the first place. If I omit the part
corresponding to

e = Tkinter.Entry()
e.pack()
b.pack(anchor=Tkinter.E)

then nothing happens, but if I include it, it's an error. Maybe you can point me
toward some kind of online resource? My favorite would be to get the knowledge
required for this one trick (invoking a button's function with a keypress
instead of a mouse click) without climbing any more of the Tkinter learning
curve (for now) than I need to.

Peace
"Jeff Epler" <je****@unpythonic.net> wrote in message
news:ma************************************@python .org...
| You'll have to arrange for the widget with keyboard focus to have a
| binding for the "<Return>" event ("<Enter>" is a valid event name, but
| it refers to the event generated when the mouse pointer enters a
| widget). The called function would call the invoke() method on the
| button.
|
| You can create a binding on all widgets within a given toplevel by
| making the binding on the toplevel itself.
|
| Example:
|
| import Tkinter
|
| def c():
| print "button invoked"
|
| t = Tkinter.Tk()
| b = Tkinter.Button(t, text="Do the thing", command=c)
| t.bind("<Return>", lambda event: b.invoke())
| e = Tkinter.Entry()
| e.pack()
| b.pack(anchor=Tkinter.E)
| t.mainloop()
|
| Jeff
|
Jul 18 '05 #5

P: n/a
It works just dandy here, and since you didn't provide the error text
there's really not much I can do for you.

Jeff

Jul 18 '05 #6

P: n/a
Sorry, that was 'my bad'. It actually does work. Thanks.

"Jeff Epler" <je****@unpythonic.net> wrote in message
news:ma************************************@python .org...
| It works just dandy here, and since you didn't provide the error text
| there's really not much I can do for you.
|
| Jeff
|
Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.