473,386 Members | 1,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

tkinter button widget

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
6 6058
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
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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: srijit | last post by:
Hello, Any idea - why the following code crashes on my Win 98 machine with Python 2.3? Everytime I run this code, I have to reboot my machine. I also have Win32all-157 installed. from Tkinter...
1
by: Pekka Niiranen | last post by:
Hi there, after reading TkInter/thread -recipe: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/82965 I wondered if it was possible to avoid using threads for the following problem: ...
2
by: Tonino | last post by:
Hi, I have a small Tkinter app that gets data from a socket connection to a "server". The app has a Text() widget to display the info that it gets from the socket connection. I have the...
7
by: Harlin Seritt | last post by:
I was looking at the Tcl/Tk sourceforge page and found that there were a couple of new widgets being produced for Tcl 8.5. Does anyone know if there are any Tkinter wrappers somewhere? thanks, ...
0
by: Stewart Midwinter | last post by:
I have a Tkinter app running on cygwin. It includes a Test menu item that does nothing more than fetch a directory listing and display it in a Toplevel window (I'd use a tkMessageBox showinfo...
2
by: Avi Kak | last post by:
Does Tkinter provide a function that returns all the event descriptors for a given widget class? I am looking for something similar to what you get in Perl/Tk when you call bind() with a single...
0
by: Svenn Bjerkem | last post by:
Hi, Armed with Programming Python 3rd Edition and Learning Python 2nd edition I try to write an application which I at first thought was simple, at least until I was finished with the GUI and...
7
by: =?iso-8859-1?q?Xavier_B=E9rard?= | last post by:
Hello everyone, I am wondering if there is a way to use custom drawing in Tkinter. I've been using it for few months, and all I know about custom drawing is to draw directly on a Canvas with...
3
by: J-Burns | last post by:
Hello. Im a bit new to using Tkinter and im not a real pro in programming itself... :P. Need some help here. Problem 1: How do I make something appear on 2 separate windows using Tkinter? By...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.