Key Event Handling using Tkinter in Python Key Event Handling using Tkinter in Python tkinter tkinter

Key Event Handling using Tkinter in Python


Tkinter events only work when the tkinter window has focus,and for it to have focus it must be visible. You cannot use tkinter to handle events while another program is in the foreground.

The format of an event is <modifier-modifier-event-detail>, with modifier and event being optional. Event is something like KeyPress, ButtonPress, ButtonRelease and so on. Detail gives more detail, such as which key, or which button. For example, <ButtonRelease-1> is for releasing mouse button 1 (one).

Modifier is where you specify control, alt, delete or shift, and you can have more than one. Shift is a bit special, because it is often interpreted by the OS before tkinter ever sees is. So, for example, "Shift-3" on an American English keyboard is "#". Thus, instead of <Shift-3> you would use <#>.

Putting that all together, command-shift-3 would be <Command-#>. However, if you do that on a Mac, it will intercept that event and do a screenshot, so the binding will only work on Windows and Linux. On each OS there are a few key bindings you cannot override.

The best description of the format to use for specifying events is the tcl/tk man page on bind. Even though you're asking about tkinter, the underlying engine is tcl/tk.