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')
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)
        msg = 'Special Key %r' % event.keysym
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)

The result is:

tkinter bind ctrl+2