How to get the clicks coordinates python 3
How to get the clicks coordinates python 3
I am making a cross-zero game on python 3.5.4 with tkinter.
I have made this at the moment:
from sys import *
from tkinter import *
screen = Tk()
c = Canvas(width=600, height=600)
c.pack()
Line1 = c.create_line(200, 0, 200, 600)
Line2 = c.create_line(400, 0, 400, 600)
Line3 = c.create_line(0, 200, 600, 200)
Line4 = c.create_line(0, 400, 600, 400)
Then, I want to make to make the main loop.
And I think that it'll be better if I make it with using clicks' coordinates.
But how can I get them?
Or maybe I should make the game with using buttons?
1 Answer
1
Here is one way to access the canvas coordinates of a mouse click:
import tkinter as tk
def click(event):
print(event.x, event.y)
if __name__ == '__main__':
screen = tk.Tk()
canvas = tk.Canvas(width=600, height=600)
canvas.pack()
canvas.bind('<Button-1>', click)
screen.mainloop()
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
THANK YOU VERY MUCH!!!
– Kostya 2007
Jul 2 at 8:54