在python gui中同时检测左右键

时间:2014-06-28 17:01:55

标签: python tkinter

尝试进行一些搜索,但是无法得到答案,但是如果有人问过这个问题,请将该帖子直接发送给我。

我在python中有一个按钮,并希望将该按钮绑定到leftClick,rightClick和bothClick函数。两个点击功能同时点击和/或释放按钮GUI上的左右鼠标按钮。

如何进行双击?

同时绑定它,以便在触发BothClick时不触发leftClick和/或rightClick。

注意:这类似于左键单击,右键单击并单击扫雷中的两个鼠标按钮。

2 个答案:

答案 0 :(得分: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()

答案 1 :(得分: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()