Tkinter (44) event


In some cases of our program, we call the event. We link the event and related parameters to a function, called binding. When the event occurs, the function will be called. The event related content is divided into three parts: event event, callback function callback and binding

Level of binding

  1. Binding components, usingbind()method
  2. Binding part categories, usingbind_class()method
  3. Binding application, usingbind_all()method

Sequence of events

  1. The sequence of events is represented by a string and contains single or multiple event patterns. Events that meet the requirements must occur in sequence. Events are usually standard input devices, such as keyboard, mouse, etc
  • Modifier such as combination keys, such as shift, Ctrl, ALT;
  • Type can be a single event, such as a letter or a key name
  • Details, such as the third mouse button
  1. Standard event sequence format, as described below


  1. example
<Button-1> type:mouse, detail:First button<KeyPress-H> type:Press the key, detail:H bond<Control-Shift-KeyPress-H> modifier:Control/Shift, type:Press the key, detail:H bond
  1. Simple expression
  • '<num>'with'<Button-num>'
  • 'x'with'<KeyPress-x>'(not applicableBlank keyand'<')

Event type of type

There are many types of events, only some common types are mentioned here

type name explain
36 Activate Parts change from inactive to usable
4 Button When the mouse button is pressed, the Linux button 4 / 5 scrolls up / down
5 ButtonRelease The mouse button is released
22 Configure The size of the part is changed
37 Deactivate Parts change from available to inactive
17 Destroy The part is being deleted
7 Enter Move the mouse into the visible part of the part
12 Expose Applications or components are exposed from being hidden by other windows
9 FocusIn When the part is focused
10 FocusOut When the component loses focus
2 KeyPress The key on the keyboard is pressed
3 KeyRelease The keys on the keyboard are released
8 Leave The mouse leaves the visible part of the component
19 Map Components appear in the application
6 Motion The mouse moves inside the component
38 MouseWheel Mouse wheel up and down, window / MacOS only
18 Unmap The parts disappear forever
15 Visibility On the screen, the window begins to be visible

Event modifier

Modifier explain
Alt When ALT key is pressed
Any General situation, such as button, mouse, etc., can not specify which key
Control When the CTRL key is pressed
Double Refers to the simultaneous occurrence of two consecutive events in a short period of time
Lock When caps lock is pressed
Shift When the shift key is pressed
Triple Refers to the simultaneous occurrence of three consecutive events in a short period of time

Key name

  • Keysym represents the string name of the character key symbol
  • Keycode key code, not affected by other auxiliary keys, such as the keycode of a and a are the same
  • keysym_ Num is equal to key symbol, and the same key will have different representative codes, such as keysym of a and a_ Num is different
  • Key keyboard text, here with"Latin-1"The code set represents the general 101 keyboard
keysym keycode keysym_num Key
Alt_L 64 65513 left alt
Alt_R 113 65514 right alt
BackSpace 22 65288 backspace
Cancel 110 65387 break
Caps_Lock 66 65549 CapsLock
Control_L 37 65507 left CTRL
Control_R 109 65508 right CTRL
Delete 107 65535 Delete
Down 104 65364
End 103 65367 end
Escape 9 65307 esc
Execute 111 65378 SysReq
F1 67 65470 F1
F2 68 65471 F2
Fi 66+i 65469+i Fi
F12 96 65481 F12
Home 97 65360 home
Insert 106 65379 insert
Left 100 65361
Linefeed 54 106 Linefeed (control-J)
KP_0 90 65438 keypad 0
KP_1 87 65436 keypad 1
KP_2 88 65433 keypad 2
KP_3 89 65435 keypad 3
KP_4 83 65430 keypad 4
KP_5 84 65437 keypad 5
KP_6 85 65432 keypad 6
KP_7 79 65429 keypad 7
KP_8 80 65431 keypad 8
KP_9 81 65434 keypad 9
KP_Add 86 65451 keypad +
KP_Begin 84 65437 keypad center (5)
KP_Decimal 91 65439 keypad .
KP_Delete 91 65439 keypad delete
KP_Divide 112 65455 keypad /
KP_Down 88 65433 keypad ↓
KP_End 87 65436 keypad end
KP_Enter 108 65421 keypad enter
KP_Home 79 65429 keypad home
KP_Insert 90 65438 keypad insert
KP_Left 83 65430 keypad ←
KP_Multiply 63 65450 keypad ×
KP_Next 89 65435 keypad PageDown
KP_Prior 81 65434 keypad PageUp
KP_Right 85 65432 keypad →
KP_Subtract 82 65453 keypad –
KP_Up 80 65431 keypad ↑
Next 105 65366 PageDown
Num_Lock 77 65407 NumLock
Pause 110 65299 pause
Print 111 65377 PrintScrn
Prior 99 65365 PageUp
Return 36 65293 enter (control-M)
Right 102 65363
Scroll_Lock 78 65300 ScrollLock
Shift_L 50 65505 left shift
Shift_R 62 65506 right shift
Tab 23 65289 tab
Up 98 65362

Event handler

  1. The parameter of the function is mainly an event object, which is used to describe some situations when an event occurs. The definition is as follows
def handler_name(event):
    """ general function """

def handler_name(self, event):
    """ general method in a class """
  1. The properties of event object are as follows, some will be set and some will not, depending on the event category
attribute explain
char In the keypress / keyrelease event, it represents a general ascii string
delta In the mousewheel event, the positive value represents the up volume, and the negative value represents the down volume; in the window, it is usually a multiple of 120; in MacOS, it is a multiple of 1
height In the configure event, represents the new height element of the part
keycode In keypress / keyrelease event, it represents keycode
keysym In the keypress / keyrelease event, it represents keysym
keysym_num In the keypress / keyrelease event, it represents keysym_ Num
num In the mouse Keyphasor event, it represents the number of the key. In Linux, the key 4 is to scroll up and the key 5 to scroll down
serial The sequence number that increases with the server processing client request events can be used to confirm the relationship between the events
state The state of the event modifier
time A time constant, increasing every MS, can be used to confirm the time difference between events
type The type code type of the event
widget The component where the event occurred
width In the configure event, represents the new element of the component
x When the event occurs, the mouse position is relative to the X coordinate of the upper left corner of the part
y When the event occurs, the mouse position is relative to the Y coordinate of the upper left corner of the part
x_root When the event occurs, the mouse position is relative to the X coordinate of the upper left corner of the screen
y_root When the event occurs, the mouse position is relative to the Y coordinate of the upper left corner of the screen
  1. Event object event attribute state, each bit represents a state
Mask Corresponding content
0x0001 Shift
0x0002 Caps Lock
0x0004 Control
0x0008 Left-hand Alt
0x0010 Num Lock
0x0080 Right-hand Alt
0x0100 Mouse button 1
0x0200 Mouse button 2
0x0400 Mouse button 3
  1. Virtual events

Add a virtual event named virtual string value named “< <…” >>”, sequence is a series of actual events. Any actual event will raise the virtual event

# event_delete(virtual, *sequences)
w.event_add('<<panic>>', '<Button-3>', '<KeyPress-Pause>')

For others, please refer to

event_delete(virtual, *sequences)
event_delete(virtual, *sequences)
event_generate(sequence, **kw)

To be continued

This work adoptsCC agreementThe author and the link to this article must be indicated in the reprint

Jason Yang