在 tkinter gui 中检测同时左击和右击 [英] Detect simultaneous left and right clicks in tkinter gui

查看:18
本文介绍了在 tkinter gui 中检测同时左击和右击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试进行了一些搜索,但无法得到答案,但是如果之前有人问过这个问题,请直接将我转至该帖子.

Tried doing some searches but was not able to get the answer however if this has been asked before kindly direct me to that post.

我在 python 中有一个按钮,想将该按钮绑定到 leftClick、rightClick 和 bothClick 函数.bothClick 功能是同时在按钮 GUI 上单击和/或释放鼠标左右键.

I have a button in python and would want to bind that button to a leftClick, rightClick and bothClick functions. bothClick function being clicking and/or releasing both right and left mouse button on the button GUI at the same time.

我怎样才能做到两个点击?

How can I do the bothClick?

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

Also bind it so that it does not trigger leftClick and/or rightClick when bothClick is triggered.

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

NOTE: this is similar to doing a left click, right click and clicking both mouse buttons in minesweeper.

推荐答案

只是对tao示例的修改.

它打印left pressright pressboth press
但只有在释放鼠标按钮时 - 有时就足够了.

It prints left pressed, right pressed and both pressed
but only when mouse buttons are released - sometimes it is enough.

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() 的版本 - 按下鼠标按钮时打印


my version with after() - it prints when mouse buttons are pressed

after() 中的 300 是反应时间".

300 in after() is 'time for reaction'.

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

这篇关于在 tkinter gui 中检测同时左击和右击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆