Muschattos
Muschattos

Reputation: 13

How to get mouse coordinates as a variable?

import tkinter as tk


field = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]


class Game:
    def click_coordinates(event):
        x = str(event.x)
        y = str(event.y)
        print(x, y)

    root = tk.Tk()
    root.title("Tic Tac Toe")
    root.geometry("304x304")
    my_canvas = tk.Canvas(root, width=300, height=300, background="black")
    my_canvas.grid(row=0, column=0)
    my_canvas.create_line(100, 0, 100, 305, fill="white", width=3)
    my_canvas.create_line(200, 0, 200, 305, fill="white", width=3)
    my_canvas.create_line(0, 100, 305, 100, fill="white", width=3)
    my_canvas.create_line(0, 200, 305, 200, fill="white", width=3)
    my_canvas.bind("<Button-1>", click_coordinates)

    root.mainloop()


if __name__ == "__main__":
    game = Game()

My problem is that I want to return the mouse coordinates as a variable. I tried to write:

def click_coordinates(event):
    x = str(event.x)
    y = str(event.y)
    mouse = x + ":" + y
    return mouse

But I have no idea how I can reach this variable with other functions. If I want to call another function from the click_coordinates() function, I get an error that the name of the new function I want to call is not defined. It seems like I can only print the coordinates.

Upvotes: 1

Views: 525

Answers (2)

user13250708
user13250708

Reputation:

i just want to advise you that you would better replace canvas by buttons,im sure that it will be easier because buttons have command, you better do something like this:

`b1=Button(win,font=("Arial Bold", 
50),width=3,bg='white',fg='SteelBlue',command=add1)
lst
b1.grid(row=2,column=1)
b2=Button(win,font=("Arial Bold", 50),width=3,bg='white',fg='SteelBlue',command=add2)
lst
b2.grid(row=2,column=2)
b3=Button(win,font=("Arial Bold", 50),width=3,bg='white',fg='SteelBlue',command=add3)
lst
b3.grid(row=2,column=3)
b4=Button(win,font=("Arial Bold", 50),width=3,bg='white',fg='SteelBlue',command=add4)
lst
b4.grid(row=3,column=1)
b5=Button(win,font=("Arial Bold", 50),width=3,bg='white',fg='SteelBlue',command=add5)
lst
b5.grid(row=3,column=2)
b6=Button(win,font=("Arial Bold", 50),width=3,bg='white',fg='SteelBlue',command=add6)
lst
b6.grid(row=3,column=3)
b7=Button(win,font=("Arial Bold", 50),width=3,bg='white',fg='SteelBlue',command=add7)
lst
b7.grid(row=4,column=1)
b8=Button(win,font=("Arial Bold", 50),width=3,bg='white',fg='SteelBlue',command=add8)
lst
b8.grid(row=4,column=2)
b9=Button(win,font=("Arial Bold", 50),width=3,bg='white',fg='SteelBlue',command=add9)
lst
b9.grid(row=4,column=3)`

and then define 9 functions and if you want to play against the computer or against another player(not good because it is very easy) and if you want to be robust just play around this code -_- .

Upvotes: 0

Bryan Oakley
Bryan Oakley

Reputation: 385870

You must set a global or instance variable if you want to access the values outside of the callback. Since you are using a class (though, using it incorrectly1), an instance variable is the natural choice:

Example:

def click_coordinates(self, event):
    self.last_x = event.x
    self.last_y = event.y

You can then reference self.last_x and self.last_y anywhere else in your object. It would be a good idea to initialize these to None in the __init__ of the class:

class Game:
    def __init__(self):
        self.last_x = self.last_y = None
        ...

1 You should move all of the code inside __init__ rather than directly under class Game. Also, click_coordinates needs a self argument and should be referenced as self.click_coordinates.

class Game:
    def __init__(self):
        root = tk.Tk()
        ...
        my_canvas.bind("<Button-1>", self.click_coordinates)

    def click_coordinates(self, event):
        ...

Upvotes: 1

Related Questions