Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Control+Number Tkinter binding

I want to bind a Control+1 sequence to a window. widget.bind("<Control-1>", lambda event: someFunction(event)) binds Control + Left Mouse Click. This is the snippet of my code that will use this:

self.master.bind("<Control-1>", lambda event: self.allTypeButtons[1].invoke())
self.master.bind("<Control-2>", lambda event: self.allTypeButtons[2].invoke())
self.master.bind("<Control-3>", lambda event: self.allTypeButtons[3].invoke())
# self.allTypeButtons is a dictionary with Radiobuttons as its values

I also tried self.master.bind("<Control>-1", lambda event: self.allTypeButtons[1].invoke()), but this gives me: _tkinter.TclError: bad event type or keysym "Control".

Also, self.master.bind("Control-1", lambda event: self.allTypeButtons[1].invoke()) and then pressing Control+1 doesn't invoke the event.

I know that widget.bind("1", lambda event: someFunction(event)) binds 1, widget.bind("<1>", lambda event: someFunction(event)) binds Left Mouse Click, and widget.bind("<Control-h>", lambda event: someFunction(event)) binds Control+h, but how can I incorporate Control+1? Thanks in advance.

like image 387
Rushy Panchal Avatar asked Jun 21 '13 17:06

Rushy Panchal


People also ask

How do I bind the Enter key to a function in Tkinter?

To bind the <Enter> key with an event in Tkinter window, we can use bind('<Return>', callback) by specifying the key and the callback function as the arguments. Once we bind the key to an event, we can get full control over the events.

What is bind () in Python?

The bind() method of Python's socket class assigns an IP address and a port number to a socket instance. The bind() method is used when a socket needs to be made a server socket. As server programs listen on published ports, it is required that a port and the IP address to be assigned explicitly to a server socket.


1 Answers

The event name is <Control-Key-1>.

import Tkinter as tk
def quit(event):
    print("You pressed Control-Key-1")
    root.quit()

root = tk.Tk()
root.bind('<Control-Key-1>', quit)
root.mainloop()

I've posted a partial table of event names here.

like image 93
unutbu Avatar answered Sep 22 '22 17:09

unutbu