Detect simultaneous left and right clicks in tkinter gui
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()
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()