python对绑定事件的鼠标、按键的判断实例
作者:x6_9x 时间:2021-05-20 03:12:58
当多个事件绑定了同一个命令,那么在命令内部根据不同的事件进行处理的时候,怎么确定哪个事件发生了呢,用下面的来检测,经过测试处理tab键和alt键不能识别,其他单个都能被识别。
还有个事件的type属性,这个经过测试键盘事件返回字符2,鼠标返回字符2,可以根据这个再进行判断反会的是键盘事件还是鼠标事件。
# <Button-1>:鼠标左击事件
# <Button-2>:鼠标中击事件
# <Button-3>:鼠标右击事件
# <Double-Button-1>:双击事件
# <Triple-Button-1>:三击事件
from tkinter import *
tk = Tk()
canvas = Canvas(width=500,height=500)
canvas.pack()
#canvas.create_polygon(0,0,250,250,fill = 'red')
def echo_event(evt):
#打印键盘事件
if evt.type == "2":
print("键盘:%s" % evt.keysym)
#打印鼠标操作
if evt.type == "4":
print("鼠标: %s" % evt.num)
#
print(evt.type)
#键盘事件
canvas.bind_all("<KeyPress>",echo_event)
#如果绑定指定的键盘,则"<Key>" 或者"<KeyPress>"都可以,具体到指定键的话后面加入下划线和指定的键就好了,如:绑定小写字母t和Left键
canvas.bind_all("<KeyPress-t>",echo_event)
canvas.bind_all("<KeyPress-Left>",echo_event)
#鼠标事件
canvas.bind_all("<Double-Button-1>",echo_event)
canvas.bind_all("<Button-1>",echo_event)
canvas.bind_all("<Button-2>",echo_event)
canvas.bind_all("<Button-3>",echo_event)
来源:https://blog.csdn.net/x6_9x/article/details/50814512
标签:python,鼠标,按键,判断
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
开心网让人很不开心
2009-04-05 15:56:00
![](https://img.aspxhome.com/file/UploadPic/20094/5/k2009331104113-27s.gif)
Python基于list的append和pop方法实现堆栈与队列功能示例
2022-10-20 02:26:26
![](https://img.aspxhome.com/file/2023/4/69014_0s.jpg)
Python中使用PIPE操作Linux管道
2022-02-10 18:25:25
![](https://img.aspxhome.com/file/2023/4/90594_0s.png)
Access 2003开发者扩展工具集概述
2009-03-19 18:32:00
使用DW中遇到的常见问题详解
2008-03-18 16:27:00
python 装饰器的使用与要点
2023-02-05 01:04:02
asp如何将数字转化成条形图?
2009-12-03 20:19:00
如何恢复/修复SQL Server的MDF文件
2008-11-24 15:30:00
pandas选择或添加列生成新的DataFrame操作示例
2023-03-02 00:41:51
python datetime时间格式的相互转换问题
2023-12-22 01:04:29
![](https://img.aspxhome.com/file/2023/7/93677_0s.jpg)
python 使用 requests 模块发送http请求 的方法
2021-06-02 17:29:19
详解Python中映射类型的内建函数和工厂函数
2022-10-15 06:24:55
python算法与数据结构之单链表的实现代码
2022-09-30 14:35:39
![](https://img.aspxhome.com/file/2023/0/98820_0s.png)
从开发人员角度看IE8的开发新特性
2010-02-26 10:48:00
python工具模块介绍之time 时间访问和转换的示例代码
2023-11-08 08:02:14
python简单猜数游戏实例
2023-10-05 21:31:55
怎样管理好css样式
2008-10-12 12:12:00
SQL的小常识, 备忘之用, 慢慢补充.
2011-11-03 17:19:45
asp如何遍历目录及目录下的文件?
2009-11-11 17:55:00
pandas获取groupby分组里最大值所在的行方法
2021-08-14 21:39:14