Tkinter's event binding

import tkinter as tk

window = tk.Tk()
# Set window size
winWidth = 600
winHeight = 400
# Get the screen resolution
screenWidth = window.winfo_screenwidth()
screenHeight = window.winfo_screenheight()

x = int((screenWidth - winWidth) / 2)
y = int((screenHeight - winHeight) / 2)

# Set the main window title
window.title ( "binding event parameter description")
# Set the initial position of the window centered on screen
window.geometry("%sx%s+%s+%s" % (winWidth, winHeight, x, y))
# Set the window icon
window.iconbitmap("./image/icon.ico")
# Set the window width and height fixed
window.resizable(0, 0)

def callBack(event):
    print("button is click")
btn = tk.Button (window, text = "click")
btn.bind("<Button-1>", callBack)
btn.pack()

def windowBack(event):
    print(event.char)
window.bind("<Key>", windowBack)

window.mainloop ()

  

First, the sequence of events

The sequence of events in the form of a string of representation, may represent one or more associated event.

The sequence of events described using the following syntax:

<modifier-type-detail>

    • Event sequences are enclosed in angle brackets (<...>) in
    • Content type is the most important part, which is often used to describe a common type of event, such as a mouse click or keyboard clicks (see below).
    • Modifier content section is optional, it is usually used to describe the combination of keys, for example, Ctrl + c, Shift + left mouse click (see below).
    • Content detail section is optional, it is often used to describe a particular key, e.g. Button-1 indicates the left mouse button.
The sequence of events meaning sequence
<Button-1> Users click on the left mouse button detail
<KeyPress-H> The user clicks the button H  
<Control-Shift-KeyPress-H> Also clicked Ctrl + Shift + H

Two, type

Activate  When the state of the component from "inactive" to "active" when the triggering event
Button  1. This event is triggered when the user clicks the mouse button when
2. detail section to specify exactly which buttons: <Button-1> left mouse button, <Button-2> mouse button, <Button-3> right mouse button, <Button- 4> roll on the roller (Linux), <Button-5 > lower rolling roller (Linux)
ButtonRelease  1. When the user releases the mouse button to trigger the thing
2. In most cases, better than Button to use, because if not careful when user presses the mouse, the user can move the mouse out components and then release the mouse, so avoid accidentally triggered event
Configure  When the size of the components of change in the triggering event
Deactivate  When the state of the component from "active" to "inactive" when the triggering event
Destroy  When the component is destroyed Fires
Enter  1. When the mouse pointer enters the component that triggered the event
2. Note: No, not the user presses the Enter key
Expose  When a part or component of the window is no longer covered by the triggering event
FocusIn  1. When the component receives focus of the event is triggered
2. The user can use the Tab key to shift the focus to the assembly (the required components takefocus option is True)
3. You can also call focus_set () method so that this component has focus
FocusOut  When the component loses focus of the event is triggered
KeyPress  1. When the user presses a key when the event is triggered
2. detail can specify a particular key, for example <KeyPress-H> represents the capital letter H is pressed when triggered when the event
3. KeyPress can be abbreviated as Key
KeyRelease  When the user releases a key trigger of the event
Leave  When the mouse pointer leaves the component that triggered the event
Map  1. When the assembly of the trigger event is mapped
2. The means is a component of the time of the application, such as calling Grid () method
Motion  When the mouse moves over the component that triggered the whole process event
MouseWheel  1. When the mouse wheel to scroll when the event is triggered
2. The event is currently only supports Windows and Mac systems, Linux systems refer to the Button
Unmap  1. When the component is unmapped Fires
2 means that the application no longer being displayed when, for example calling grid_remove () method
Visibility  When the application is at least partly visible on the screen of the trigger event

Three, modifier

Alt When pressing the Alt key
Any  1 represents any type of key is pressed when
2. e.g. <Any-KeyPress> represents a trigger event when a user presses any key
Control  When you press the Ctrl key when
Double  1. 当后续两个事件被连续触发的时候
2. 例如 <Double-Button-1> 表示当用户双击鼠标左键时触发事件
Lock   当打开大写字母锁定键(CapsLock)的时候
Shift  当按下 Shift 按键的时候
Triple  跟 Double 类似,当后续三个事件被连续触发的时候

 四、Event 对象

widget  产生该事件的组件
x, y 当前的鼠标位置坐标(相对于窗口左上角,像素为单位)
x_root, y_root  当前的鼠标位置坐标(相对于屏幕左上角,像素为单位)
keysym    按键名,见下方 Key names(键盘事件专属)
keycode  按键码,见下方 Key names(键盘事件专属)
num  按钮数字(鼠标事件专属)
width, height 组件的新尺寸(Configure 事件专属)

type  该事件类型

五、Key names

按键名(keysym) 按键码(keycode) 代表的按键
Alt_L 64 左边的 Alt 按键
Alt_R 113 右边的 Alt 按键
BackSpace 22 Backspace(退格)按键
Cancel 110 break 按键
Caps_Lock 66 CapsLock(大写字母锁定)按键
Control_L 37 左边的 Ctrl 按键
Control_R 109 右边的 Ctrl 按键
Delete 107 Delete 按键
Down 104 ↓ 按键
End 103 End 按键
Escape 9 Esc 按键
Execute 111 SysReq 按键
F1 67 F1 按键
F2 68 F2 按键
F3 69 F3 按键
F4 70 F4 按键
F5 71 F5 按键
F6 72 F6 按键
F7 73 F7 按键
F8 74 F8 按键
F9 75 F9 按键
F10 76 F10 按键
F11 77 F11 按键
F12 96 F12 按键
Home 97 Home 按键
Insert 106 Insert 按键
Left 100 ← 按键
Linefeed 54 Linefeed(Ctrl + J)
KP_0 90 小键盘数字 0
KP_1 87 小键盘数字 1
KP_2 88 小键盘数字 2
KP_3 89 小键盘数字 3
KP_4 83 小键盘数字 4
KP_5 84 小键盘数字 5
KP_6 85 小键盘数字 6
KP_7 79 小键盘数字 7
KP_8 80 小键盘数字 8
KP_9 81 小键盘数字 9
KP_Add 86 小键盘的 + 按键
KP_Begin 84 小键盘的中间按键(5)
KP_Decimal 91 小键盘的点按键(.)
KP_Delete 91 小键盘的删除键
KP_Divide 112 小键盘的 / 按键
KP_Down 88 小键盘的 ↓ 按键
KP_End 87 小键盘的 End 按键
KP_Enter 108 小键盘的 Enter 按键
KP_Home 79 小键盘的 Home 按键
KP_Insert 90 小键盘的 Insert 按键
KP_Left 83 小键盘的 ← 按键
KP_Multiply 63 小键盘的 * 按键
KP_Next 89 小键盘的 PageDown 按键
KP_Prior 81 小键盘的 PageUp 按键
KP_Right 85 小键盘的 → 按键
KP_Subtract 82 小键盘的 - 按键
KP_Up 80 小键盘的 ↑ 按键
Next 105 PageDown 按键
Num_Lock 77 NumLock(数字锁定)按键
Pause 110 Pause(暂停)按键
Print 111 PrintScrn(打印屏幕)按键
Prior 99 PageUp 按键
Return 36 Enter(回车)按键
Right 102 → 按键
Scroll_Lock 78 ScrollLock 按键
Shift_L 50 左边的 Shift 按键
Shift_R 62 右边的 Shift 按键
Tab 23 Tab(制表)按键
Up 98 ↑ 按键

Guess you like

Origin www.cnblogs.com/yang-2018/p/11797151.html