简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM