python gui之tkinter事件处理
| 事件 | 代码 | 备注 | 
| 鼠标左键单击按下 | 1/Button-1/ButtonPress-1 | |
| 鼠标左键单击松开 | ButtonRelease-1 | |
| 鼠标右键单击 | 3 | |
| 鼠标左键双击 | Double-1/Double-Button-1 | |
| 鼠标右键双击 | Double-3 | |
| 鼠标滚轮单击 | 2 | |
| 鼠标滚轮双击 | Double-2 | |
| 鼠标移动 | B1-Motion | |
| 鼠标移动到区域 | Enter | |
| 鼠标离开区域 | Leave | |
| 获得键盘焦点 | FocusIn | |
| 失去键盘焦点 | FocusOut | |
| 键盘事件 | Key | |
| 回车键 | Return | |
| 控件尺寸变 | Configure | 
响应时间
提前响应
ttk treeview的TreeviewSelect事件是提前的,即你选中了某行,通过treeview.selection()[0]得到的就是这一样。
延后相应
比如ttk的treeview是的单击的情况,单击的行被选中了,但是通过 treeview.selection()[0]得到的却不是选中的行!而是之前选中的行。可以参考下这里。
响应函数
event_handler(event,*args)
event参数
event 参数有以下属性:
['__doc__', '__module__', 'char', 'delta', 'height', 'keycode', 'keysym', 'keysym_num', 'num', 'send_event', 'serial', 'state', 'time', 'type', 'widget', 'width', 'x', 'x_root', 'y', 'y_root']
Event Attributes
- widget
- 
The widget which generated this event. This is a valid Tkinter widget instance, not a name. This attribute is set for all events. 
- x, y
- 
鼠标当前的相对位置,以像素为单位。 比如,ttk treeview 有个通过y坐标定位行的方法:identify_row(self, y) 
- x_root, y_root
- 
鼠标当前的绝对位置(相对于设备的左上角)。以像素为单位。 
- char
- 
字符(键盘事件中才有), 类型是字符串。 
- keysym
- 
The key symbol (keyboard events only). 键符(键盘事件中才有) 
- keycode
- 
键码 (键盘事件中才有). 
- num
- 
按钮号码(鼠标事件中才有)1-左键/2-中/3-右 
- width, height
- 
widget的新尺寸,以像素为单位(Configure events only). 
- type
- 事件类型
- 1---
- 2---
- 3---
- 4---鼠标
- 更多资料参考这里。
绑定事件
控件.bind('<事件代码>',event_handler)
适用于大多数控件。此外还有bind_all方法。
控件.protocal('事件代码', event_handler)
这种情况的控件,必需是顶层窗口或者root容器。

 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号