I'm trying to design a function that will display the coordinates of the canvas so i wrote this function inside another function which is the canvas for the solar system simulation. Here is my code:
def solar_system():
top2 = Toplevel()
top2.title("Solar system simulation") #window title
canvas = tk.Canvas(top2, width=1500, height=900, bg = "black") #making the canvas
canvas.pack()
def display_xy_coordinates(event): #function for displaying x and y
widgetxy["text"]=f"x,{event.x} y,{event.y}"
display_xy_coordinates(event)
widgetxy = Label(canvas, font = "Calibri 10", fg = "white", bg = "black") #colour and font for label showing xy coordinates
canvas.bind("<Button-1>", display_xy_coordinates) #Buton-1 = left click on the mouse. This calls the display xy coordinates function
widgetxy.pack() #this label needs to be packed in order to show
canvas.create_window(1267, 34, window=widgetxy) #this is where the label will be placed (top right of canvas)
I have tried every possible way of writing this function inside the main function but i always get this error:
display_xy_coordinates(event)
^^^^^
NameError: name 'event' is not defined
source https://stackoverflow.com/questions/75502621/how-can-i-nest-a-function-inside-an-outer-function
Comments
Post a Comment