网络编程
位置:首页>> 网络编程>> Python编程>> python对绑定事件的鼠标、按键的判断实例

python对绑定事件的鼠标、按键的判断实例

作者:x6_9x  发布时间:2021-05-20 03:12:58 

标签:python,鼠标,按键,判断

当多个事件绑定了同一个命令,那么在命令内部根据不同的事件进行处理的时候,怎么确定哪个事件发生了呢,用下面的来检测,经过测试处理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

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com