I want to inform user if they clicked in a specific section of root window.
import tkinter as tk
# Setting up the window
root=tk.Tk()
root.geometry("400x250")
root.resizable(width=False,height=False)
''' If the user clicked in a specific
section of root window, then
the terminal will inform user '''
RightOnPoint = lambda event,x,x0,y,y0: print("You clicket right on point!") if x-x0>132 and x-x0<300 and y-y0>15 and y-y0<36 else None
root.bind("<Button-1>", lambda event,root.winfo_pointerx(),root.winfo_rootx(),root.winfo_pointery(),root.winfo_rooty(): RightOnPoint)
# Mainloop
root.mainloop()
using bind to associate mouse left-click. And to get absolute mouse coordinate I use something like this:
root.winfo_pointerx()-root.winfo_rootx()
where root.winfo_pointerx()
is represented by x
and the value of root.winfo_rootx()
is assigned to x0 and so on.
and I'm getting an "Invalid syntax Error", although I cannot understand what I have done wrong.
I tried to define a function instead of using a lambda function like such:
def RightOnPoint():
x=root.winfo_pointerx()
x0=root.winfo_rootx()
y=root.winfo_pointery()
y0=root.winfo_rooty()
if x-x0>132 and x-x0<300 and y-y0>15 and y-y0<36:
print("You clicket right on point!")
else:
pass
root.bind("<Button-1>", lambda event: RightOnPoint())
The RightOnPont()
function expects one positional argument. That argument is event information
. So do it as the following:
import tkinter as tk
# Setting up the window
root=tk.Tk()
root.geometry("400x250")
root.resizable(width=False,height=False)
''' If the user clicked in a specific
section of root window, then
the terminal will inform user '''
def RightOnPoint(event):
x=root.winfo_pointerx()
x0=root.winfo_rootx()
y=root.winfo_pointery()
y0=root.winfo_rooty()
if x-x0>132 and x-x0<300 and y-y0>15 and y-y0<36:
print("You clicket right on point!")
else:
pass
root.bind("<Button-1>", lambda event: RightOnPoint)
# RightOnPoint = lambda event,x,x0,y,y0: print("You clicket right on point!") if x-x0>132 and x-x0<300 and y-y0>15 and y-y0<36 else None
root.bind("<Button-1>",RightOnPoint)
# Mainloop
root.mainloop()