DRTauli
DRTauli

Reputation: 771

Detect simultaneous left and right clicks in 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.

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?

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.

Upvotes: 0

Views: 2295

Answers (3)

user16435384
user16435384

Reputation:

self.bind("<Button-3>", lambda e:self.func())

Upvotes: 0

furas
furas

Reputation: 142631

It is only modification of tao example.

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

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

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

Upvotes: 1

bhaskarc
bhaskarc

Reputation: 9521

You can achieve this is as follows.

Use two variables left_mouse_pressed and right_mouse_pressed. When both are simultaneously True, both mouse keys pressed.

Reset their state to False on mouse release.

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

Upvotes: 2

Related Questions