繁体   English   中英

在 tkinter gui 中检测同时左击和右击

[英]Detect simultaneous left and right clicks in tkinter gui

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

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

我该怎么做bothClick?

还要绑定它,以便在触发 bothClick 时它不会触发 leftClick 和/或 rightClick。

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

您可以实现如下。

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

只是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()
self.bind("<Button-3>", lambda e:self.func())

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM