Answer the question
In order to leave comments, you need to log in
How to know x or y on mouse click?
Here a window appears and when the mouse clicks on the screen, for example, the following line appears:
ButtonPress event num=1 x=285 y=120
this line contains x and y. How to make x or y turn into a variable.
Here is the code:
from tkinter import *
window = Tk()
c = Canvas(width = 500, height = 500)
c.pack()
def x(event):
print(event)
c.bind_all("<Button-1>", x)
window.mainloop()
Answer the question
In order to leave comments, you need to log in
from tkinter import *
window = Tk()
c = Canvas(width = 500, height = 500)
c.pack()
def x(event):
x = event.x
y = event.y
print(f'x={x} y={y}')
c.bind_all("<Button-1>", x)
window.mainloop()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question