473,761 Members | 4,421 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6087
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=T kinter.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.492 595$Ig.75690@pd 7tw2no>...
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_some thing)
b.bind('<Return >', lambda event, key='<space>' : b.event_generat e(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=T kinter.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****@unpytho nic.net> wrote in message
news:ma******** *************** *************@p ython.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=T kinter.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****@unpytho nic.net> wrote in message
news:ma******** *************** *************@p ython.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
7035
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 import * class App:
1
2253
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: I have script started from W2K console that normally prints ascii messages to the screen. However, I have command line "debug" -flag that might cause printing
2
2951
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 ability to stop the text at any point. What I want to be able todo is select a line from the Text() window and double click or whatever on it to open a new window with that selected text as a paramater to the new window.
7
4539
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, Harlin
0
2359
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 widget, but for some reason the text is invisible on cygwin). After I close the Toplevel widget, all of the menus in my app behave as though they have no contents to them, i..e I can press on the File menu button, and see it depress, but the Exit...
2
1450
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 explicit argument. For example, in Perl/Tk, $widget->bind( Tk::Button ) returns a list like <Key-Return>
0
1425
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 then wanted to start putting some data into it. As you will see, the program is not yet finished but I post what I have so far.
7
2318
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 such methods as "create_line", "create_rectangle", etc. Now, the problem, is that I have already plenty of widgets on my screen. I just want to draw over them, which is a bit difficult in my comprehension of things.
3
3918
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 this I mean that the format would be something like this: You have Page1 : This has 2-3 buttons on it. Clicking on each button opens up a new window respectively having
0
9538
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10123
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9909
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9788
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6623
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5241
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5384
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3889
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2765
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.