The event object#
A callback bound with bind receives one argument: an event object whose
attributes describe what happened. tkinter fills these from the underlying Tk
event; an attribute that does not apply to the event type holds the string
"??" (or a meaningless value), so read only the ones relevant to the event
you bound.
Attribute |
Tk field |
Meaning |
Populated for |
|---|---|---|---|
|
|
The widget instance the event fired on. |
all events |
|
|
Pointer position relative to the widget’s top-left. |
pointer & key events |
|
|
Pointer position relative to the whole screen. |
pointer & key events |
|
|
Button number (1–5). |
Button events |
|
|
Wheel rotation amount and direction. |
MouseWheel |
|
|
The character produced ( |
key events |
|
|
The key symbol name ( |
key events |
|
|
The key symbol as a number. |
key events |
|
|
The hardware key code. |
key events |
|
|
Modifier/button state as a bitmask integer (see below). |
pointer, key & other events |
|
|
Server timestamp in milliseconds. |
most events |
|
|
New size of the widget. |
Configure, Expose |
|
|
The event type as an |
all events |
|
|
Whether the pointer’s window has focus. |
Enter, Leave |
|
|
|
all events |
|
|
The event’s serial number. |
all events |
event.type#
event.type is an EventType enum member, not a bare string. Read its name
or compare against the enum:
import tkinter as tk
def handler(event):
print(event.type.name) # -> "ButtonPress"
if event.type == tk.EventType.KeyPress:
...
event.state#
state is a bitmask of the modifiers and buttons held when the event fired
(Shift = 0x0001, Lock = 0x0002, Control = 0x0004,
Button1 = 0x0100, …). Decoding it by hand is rarely worth it — prefer
naming the modifier in the pattern (<Shift-Button-1>) so tkinter matches it
for you.
Fields tkinter does not expose#
Tk defines several event fields that tkinter’s event object omits — notably
the detail / user-data field (%d) of a virtual event. This means a value
passed to event_generate(..., data="…") is not readable on the Python
event; to pass information alongside a
virtual event, keep it somewhere both sides can see (an
attribute, a variable) rather than on the event. The X11-internal fields (%a,
%c, %m, %o, %p, %B, %P, %R, %S, %i) are
likewise not exposed.