|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
tkinter 如何区分鼠标双击和单击?
- import tkinter as tk
- root=tk.Tk()
- l=tk.Label(root,text='鼠标按键测试',width=15,height=5)
- l.pack()
- n=0
- def 单击(event):
- global n
- n+=1
- print('单击',n)
- d=0
- def 双击(event):
- global d
- d+=1
- print('双击',d)
-
- l.bind("<Button-1>",单击)
- l.bind("<Double-Button-1>",双击)
- root.mainloop()
复制代码
问题是:在对lable双击鼠标时,总是先触发一次单击,再触发双击事件。
我想要的效果是,双击时只触发 双击事件。如何严格把单双击事件分开?
7天无人能解~
自己想了个办法
用一个 过滤线程 把多次按键动作分辨出来~
- import tkinter as tk
- import time
- import threading as thd
- ClkCnt=[0,0,0]
- lock = thd.Lock()
- lcClk = thd.local()
- def 过滤器线程(clk):
- lcClk.clk=clk
- slp=time.sleep
- tm=time.time
- t=tm()
- while tm()-t<0.3: #按键间隔
- lock.acquire()
- if ClkCnt[lcClk.clk-1]==0 :
- lock.release()
- return
- if max(ClkCnt)> lcClk.clk :
- ClkCnt[lcClk.clk-1]=0
- lock.release()
- return
-
- lock.release()
- slp(0.01)
- print('单双叁'[lcClk.clk-1],'击')
- ClkCnt[lcClk.clk-1]=0
- return
-
- def 单(event):
- if ClkCnt[0]==0 :
- ClkCnt[0]=1
- thd.Thread(target=过滤器线程,args=(1,)).start()
- def 双(event):
- if ClkCnt[1]==0 :
- ClkCnt[1]=2
- thd.Thread(target=过滤器线程,args=(2,)).start()
-
- def 叁(event):
- if ClkCnt[2]==0 :
- ClkCnt[2]=3
- thd.Thread(target=过滤器线程,args=(3,)).start()
- ##------------------
- root=tk.Tk()
- l=tk.Label(root,text='鼠标按键测试',width=15,height=5)
- l.pack()
- l.bind("<Button-1>",单)
- l.bind("<Double-Button-1>",双)
- l.bind("<Triple-Button-1>",叁)
- root.mainloop()
复制代码
|
|