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容器。

转载于:https://www.cnblogs.com/Tommy-Yu/p/4170448.html

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值