K
K
Kozhanov Maxim2020-06-04 16:57:18
Python
Kozhanov Maxim, 2020-06-04 16:57:18

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

Help please =)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Karbivnichy, 2020-06-04
@MaxPython375

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 question

Ask a Question

731 491 924 answers to any question