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


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

