Python Tkinter Bind Ctrl+Key – Python Tutorial

By | June 17, 2019

Python tkinter can bind keys and call some functions. If you want to bind Ctrl+Key, you should use: Control-Key-key

For example, if you want to bind Ctrl+1, Ctrl+c and Ctrl + /, you can refer this example.

from tkinter import *
root = Tk()
prompt = '      Press any key      '
label1 = Label(root, text=prompt, width=len(prompt), bg='yellow')
label1.pack()
def key(event):
    if event.char == event.keysym:
        msg = 'Normal Key %r' % event.char
    elif len(event.char) == 1:
        msg = 'Punctuation Key %r (%r)' % (event.keysym, event.char)
    else:
        msg = 'Special Key %r' % event.keysym
    label1.config(text=msg)
root.bind_all('<Control-Key-C>', key)
root.bind_all('<Control-Key-1>', key)
root.bind_all('<Control-Key-2>', key)
root.bind_all('<Control-Key-/>', key)
root.mainloop()

The result is:

tkinter bind ctrl+2

Leave a Reply

Your email address will not be published. Required fields are marked *