2014-06-28 768 views
0

嘗試做一些搜索,但無法得到答案,但如果這之前已經問過,請親切指示我到那個帖子。在python中同時檢測左鍵和右鍵gui

我在python中有一個按鈕,並希望將該按鈕綁定到leftClick,rightClick和bothClick函數。 bothClick功能同時在按鈕GUI上單擊和/或釋放鼠標右鍵和左鍵。

我該如何做雙擊?

還綁定它,以便在觸發雙擊時不會觸發leftClick和/或rightClick。

注意:這與左擊,右擊並單擊掃雷中的兩個鼠標按鈕類似。

+0

在'leftClick'等一下,檢查鼠標是否點擊,然後做左鍵單擊或雙擊單擊。在'rightClick'稍等片刻,檢查是否鼠標左鍵點擊,然後做右鍵點擊或什麼都不做。 – furas

回答

1

它只是對tao示例的修改。

它打印left pressedright pressedboth 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() 
+0

太棒了。謝謝。因爲沒有考慮使用國旗,所以得到了自己的頭腦。 – DRTauli

2

你可以做到這一點如下。

使用兩個變量left_mouse_pressedright_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() 
+0

如何使用此代碼獲得'左按','右按'和'雙按'? – furas