To handle keyboard events in a Python GUI, register a handler for key presses on the widget or window that has focus. In Tkinter you call bind("<KeyPress>", handler) and read the keysym of the event. In Qt (PyQt6 or PySide6) you override keyPressEvent or use QShortcut and QKeySequence for shortcuts.
Both toolkits deliver events only to the widget that currently has keyboard focus, which is the most common reason a handler "does nothing". The sections below show a runnable example for each toolkit, how to detect specific keys and modifiers, and how to avoid the usual focus problems.
A GUI toolkit runs an event loop. When you press a key, the operating system sends the event to the focused window, the toolkit finds the focused widget, and it calls your handler. If no widget has focus, nothing is called.
| Toolkit | Mechanism | Key identifier | Shortcut API |
|---|---|---|---|
| Tkinter | widget.bind(sequence, callback) | event.keysym, event.char | bind("<Control-s>", ...) |
| PyQt6 / PySide6 | Override keyPressEvent(event) | event.key(), event.text() | QShortcut(QKeySequence(...), parent) |
Tkinter ships with Python. PyQt5 is still widely installed, but Qt 5 is end of life, so new projects should use PyQt6 or PySide6 (pip install PyQt6). The code in this article targets PyQt6, where enums are fully scoped, for example Qt.Key.Key_Return.
Use bind on the root window to receive every key press while the window is focused. The callback gets an event object that describes the key.
Pressing a then Enter prints output like this:
The useful attributes are:
event.keysym: a symbolic name such as a, Return, Escape, Left or F5. Use it to identify keys.event.char: the character produced, or an empty string for keys such as Shift or arrows.event.state: a bit mask of modifiers such as Shift and Control.<KeyPress> fires on every key down and repeats while the key is held. <KeyRelease> fires when the key is released. Older tutorials, including an earlier version of this answer, say <Key> means key release. That is wrong: <Key> is simply a shorthand for <KeyPress>.
Bind a specific sequence to its own handler instead of writing a long if chain. Sequences are written in angle brackets, with modifiers first.
Keep these rules in mind:
<Control-s> and <Control-S> are different sequences, and the second one fires when Caps Lock or Shift is active.widget.focus_set() for widgets such as Canvas or Frame, which do not take focus by default.root.bind_all(...) to catch a key anywhere in the application, or bind_class for all widgets of one class."break" from a handler to stop further handlers from running.In Qt, a widget receives key events through keyPressEvent and keyReleaseEvent. Override them in a subclass, and call the base class implementation for keys you do not handle so normal behaviour keeps working.
The important points are:
event.key() returns a Qt.Key value that does not depend on the keyboard layout or on Shift. Compare against Qt.Key.Key_Return for the main Enter key and Qt.Key.Key_Enter for the numeric keypad.event.text() returns the typed text, which is empty for non-printing keys.event.modifiers() returns flags you test with &.setFocusPolicy(Qt.FocusPolicy.StrongFocus) lets the label take focus. A plain QLabel ignores the keyboard by default, which is why the original example set it.For commands such as Save or Quit, prefer shortcuts over overriding keyPressEvent. Qt handles platform conventions (Ctrl on Windows and Linux, Cmd on macOS) and conflicts for you.
Note that in PyQt6 QShortcut and QAction live in QtGui, while in PyQt5 they were in QtWidgets. QKeySequence.StandardKey gives you the correct key combination for each operating system.
| Symptom | Cause | Fix |
|---|---|---|
| Handler never runs | Widget does not have focus | Call focus_set() in Tkinter or set a focus policy and call setFocus() in Qt |
| Runs twice in Qt | Parent and child both handle the event | Call event.accept() when you handled it, otherwise super().keyPressEvent(event) |
| Key held fires repeatedly | Auto-repeat | In Qt check event.isAutoRepeat(); in Tkinter track state yourself |
| Wrong key for non-English layout | Using char or text() | Compare keysym or event.key() |
| Arrow keys move focus instead | Qt or Tk default navigation | Override the handler on the focused widget, or bind to <Left> and return "break" |
keysym is the symbolic name of the key, such as Return or F1, and it is set for every key. char is the text the key produced, and it is empty for non-printing keys such as arrows.
Use PyQt6 or PySide6. Qt 5 has reached end of life, and Qt 6 is the supported line. Moving code mostly means using scoped enums such as Qt.Key.Key_Escape and app.exec().
In Tkinter bind <Control-s>. In Qt create a QShortcut with QKeySequence("Ctrl+S"), or check event.modifiers() inside keyPressEvent.
Standard GUI toolkits cannot do that, because the operating system only delivers events to the focused window. Global hotkeys need an OS-level library and are platform specific.
Keyboard handling in Python GUIs comes down to focus and the right API. In Tkinter, use bind with <KeyPress> or a specific sequence and read event.keysym. In PyQt6 or PySide6, override keyPressEvent, compare event.key() and event.modifiers(), and use QShortcut for application commands. Make sure the widget can take focus, and always call the base class for keys you do not handle.