![](/img/trans.png)
[英]tkinter left clicks on a TAB in your GUI. How to detect when user has done this
[英]Detect simultaneous left and right clicks in tkinter gui
嘗試進行一些搜索,但無法得到答案,但是如果之前有人問過這個問題,請引導我到那個帖子。
我在 python 中有一個按鈕,並且希望將該按鈕綁定到 leftClick、rightClick 和 bothClick 函數。 bothClick 函數同時在按鈕 GUI 上單擊和/或釋放鼠標左右鍵。
我該怎么做bothClick?
還要綁定它,以便在觸發 bothClick 時它不會觸發 leftClick 和/或 rightClick。
注意:這類似於在掃雷中進行左鍵單擊、右鍵單擊並單擊兩個鼠標按鈕。
您可以實現如下。
使用兩個變量left_mouse_pressed
和right_mouse_pressed
。 當兩者同時為True
,都按下了兩個鼠標鍵。
釋放鼠標時將其狀態重置為False
。
import Tkinter
class App:
def __init__(self, root):
self.root = root
self.left_mouse_pressed = False
self.right_mouse_pressed = False
f = Tkinter.Frame(width=100, height=100, background="cyan")
f.pack()
f.bind("<Button-1>", self.onAnyofTwoPressed)
f.bind("<Button-3>", self.onAnyofTwoPressed)
f.bind("<ButtonRelease-1>", self.resetPressedState)
f.bind("<ButtonRelease-3>", self.resetPressedState)
def onAnyofTwoPressed(self, event):
if event.num==1:
self.left_mouse_pressed = True
if event.num==3:
self.right_mouse_pressed = True
if (self.left_mouse_pressed and self.right_mouse_pressed):
print 'yay both pressed'
def resetPressedState(self, event):
self.left_mouse_pressed = False
self.right_mouse_pressed = False
root=Tkinter.Tk()
app = App(root)
root.mainloop()
只是tao示例的修改。
它打印left pressed
, right pressed
和both pressed
但僅在釋放鼠標按鈕時-有時就足夠了。
import Tkinter
import time
class App:
def __init__(self, root):
self.root = root
self.left_mouse_pressed = False
self.right_mouse_pressed = False
f = Tkinter.Frame(width=100, height=100, background="cyan")
f.pack()
f.bind("<Button-1>", self.onAnyofTwoPressed)
f.bind("<Button-3>", self.onAnyofTwoPressed)
f.bind("<ButtonRelease-1>", self.resetPressedState)
f.bind("<ButtonRelease-3>", self.resetPressedState)
def onAnyofTwoPressed(self, event):
if self.left_mouse_pressed and self.left_mouse_pressed <= time.time():
self.left_mouse_pressed = False
if self.right_mouse_pressed and self.right_mouse_pressed <= time.time():
self.right_mouse_pressed = False
if event.num==1:
self.left_mouse_pressed = time.time() + 500
if event.num==3:
self.right_mouse_pressed = time.time() + 500
def resetPressedState(self, event):
if self.left_mouse_pressed and self.right_mouse_pressed:
print 'both pressed'
elif self.left_mouse_pressed:
print 'left pressed'
elif self.right_mouse_pressed:
print 'rigth pressed'
self.left_mouse_pressed = False
self.right_mouse_pressed = False
root=Tkinter.Tk()
app = App(root)
root.mainloop()
編輯:我的版本與after()
-按下鼠標按鈕時打印
after()
300是“反應時間”。
import Tkinter as tk
import time
root = tk.Tk()
left_pressed = False
rigth_pressed = False
def on_left_click(event):
global left_pressed, rigth_pressed
if rigth_pressed:
rigth_pressed = False
else:
left_pressed = True
root.after(300, on_left_later)
def on_left_later():
global left_pressed
if left_pressed:
left_pressed = False
print "left pressed"
else:
print "both pressed"
def on_right_click(event):
global left_pressed, rigth_pressed
if left_pressed:
left_pressed = False
else:
rigth_pressed = True
root.after(300, on_right_later)
def on_right_later():
global rigth_pressed
if rigth_pressed:
rigth_pressed = False
print "rigth pressed"
# else:
# print "(right_do_nothing)"
button = tk.Button(root, text="Clik me! - left, right, both")
button.pack()
button.bind('<Button-1>',on_left_click)
button.bind('<Button-3>',on_right_click)
tk.mainloop()
self.bind("<Button-3>", lambda e:self.func())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.