Tkinter’s binding of Keys, listbox etc.

Tkinter Example – The use of key binding

Tkinter has a bind method for every widget to run an action defined into a function or a method when the user does something.

In the following example when the user hits any key, on the console will appear the updated number of times that a key has been pressed. The first arg to pass to the bind method is the type of action. In this case it is <Key> and it means that the function (key) will be called everytime the user hits a key

import tkinter as tk


keyspressed = 0
def key():
    global keyspressed
    print(keyspressed)
    keyspressed += 1

root = tk.Tk()
root.bind("<Key>", key)
label = tk.Label(root, text="Press a key and look at the console")
label.pack()
root.mainloop()

Press one key

This is an example with one key

import tkinter as tk


keyspressed = 0
def key():
    global keyspressed
    print(f"Pressed c: {keyspressed} times")
    keyspressed += 1

root = tk.Tk()
root.bind("<c>", lambda x: key())
label = tk.Label(root, text="Press a key and look at the console")
label.pack()
root.mainloop()

Can I bind to ctrl + a key? Yes!

That’s how you do, you use the keyword Contro-c for the ctrl+c combination. The same for the other keys.

import tkinter as tk


keyspressed = 0
def key():
    global keyspressed
    print(f"Pressed ctrl+c: {keyspressed} times")
    keyspressed += 1

root = tk.Tk()
root.bind("<Control-c>", lambda x: key())
label = tk.Label(root, text="Press a key and look at the console")
label.pack()
root.mainloop()

 

Press a key and show on the console without showing the window

If you want to get the user input on the console, but you do not want or need to see the tkinter window, you can use withdraw to do it, like you can see in the example below. You will not see anything when you’ll launch the script, but if you press the “c” key you will see the text on the console saying that you pressed the c key a number of times.

import tkinter as tk


keyspressed = 0
def key():
    global keyspressed
    print(f"Pressed c: {keyspressed} times")
    keyspressed += 1

root = tk.Tk()
root.withdraw()
root.focus()
root.bind("<c>", lambda x: key())
label = tk.Label(root, text="Press a key and look at the console")
label.pack()
root.mainloop()
root.destroy()

 

A partial list of the key binding keywords

| event | name | 
| Ctrl-c | Control-c | 
| Ctrl-/ | Control-slash | 
| Ctrl-\ | Control-backslash | 
| Ctrl+(Mouse Button-1) | Control-1 | 
| Ctrl-1 | Control-Key-1 | 
| Enter key | Return 
            | Button-1 |
            | ButtonRelease-1 |
            | Home |
            | Up, Down, Left, Right |
            | Configure |
| window exposed | Expose | 
| mouse enters widget | Enter | 
| mouse leaves widget | Leave |
                      | Key |
                      | Tab |
                      | space |
                      | BackSpace |
                      | KeyRelease-BackSpace |
| any key release     | KeyRelease | 
| escape              | Escape |
	              | F1 |
	              | Alt-h |

Other keywords for other events and widgets

The listbox

When the user select an item in the listbox

<<ListBoxSelect>>

This listener wait for the user to select an item

import tkinter as tk


def selected():
    print(listbox.get(listbox.curselection()[0]))

root = tk.Tk()
listbox = tk.Listbox(root)
listbox.pack()
for i in range(10):
	listbox.insert(0,i)
listbox.bind("<<ListboxSelect>>", lambda x: selected())
root.mainloop()
When user clicks on the item it is printed on the console

Wait for the user to doubleclick on an item

<Double-Button>

 

Tkinter test for students

Tkinter articles

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.