【问题标题】:Detect simultaneous left and right clicks in tkinter gui在 tkinter gui 中检测同时左击和右击
【发布时间】:2014-08-19 13:59:27
【问题描述】:

尝试进行一些搜索,但无法得到答案,但是如果之前有人问过这个问题,请引导我到那个帖子。

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

我怎样才能同时点击?

还要绑定它,这样当bothClick被触发时它不会触发leftClick和/或rightClick。

注意:这类似于在扫雷中进行左键单击、右键单击和同时单击鼠标按钮。

【问题讨论】:

  • leftClick 稍等片刻,检查是否单击了鼠标右键,然后单击左键或单击鼠标右键。在rightClick 稍等片刻,检查鼠标左键是否被单击,然后执行右键单击工作或不执行任何操作。

标签: python tkinter


【解决方案1】:

你可以实现如下。

使用两个变量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()

【讨论】:

  • 如何使用此代码获取left pressedright pressedboth pressed
【解决方案2】:

只是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()

【讨论】:

  • 太棒了。谢谢。因为没有考虑使用旗帜,所以我不得不把自己撞到头上。
【解决方案3】:
self.bind("<Button-3>", lambda e:self.func())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-20
    • 2023-03-13
    相关资源
    最近更新 更多