tkinter button widget

E

Elaine Jackson

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
 
J

Jeff Epler

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
 
J

Jeff Epler

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

Jeff
 
K

klappnase

Elaine Jackson said:
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
 
E

Elaine Jackson

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


| 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
|
 
J

Jeff Epler

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
 
E

Elaine Jackson

Sorry, that was 'my bad'. It actually does work. Thanks.

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

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top