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()得到的就是这一样。
延后相应
比如ttk的treeview是的单击的情况,单击的行被选中了,但是通过 treeview.selection()得到的却不是选中的行!而是之前选中的行。可以参考下这里。
响应函数
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容器。
页:
[1]