does anyone know why my width is not defined, it seems that i have

from tkinter import *
import random
root=Tk()
root.geometry("400x400")

canvas = Canvas(root,width= width,height= height, bg='black')
canvas.pack(pady=20)
width,height=200,200
shape=canvas.create_circle(fill=yellow)

def pressed(event):
    index=random.randrange(0,5)
    x,y=0,0
    if event.char == "a":
        x,y =-10,0
    if event.char == "d":
        x,y =10,0w
    if event.char == "w":
        x,y =0,-10
    if event.char == "s":
        x,y =-0,10
root.bind("",pressed)

root.mainloop()

I think I have already defined width and height but I still get the error.This is why i am asking for help. Please answer!! :))

>Solution :

You are trying to use width and height before defining it. Just initialize width and height before using it, and you are good to go.

from tkinter import *
import random
root=Tk()
root.geometry("400x400")

width,height=200,200
canvas = Canvas(root,width= width,height= height, bg='black')
canvas.pack(pady=20)
shape=canvas.create_circle(fill=yellow)

def pressed(event):
    index=random.randrange(0,5)
    x,y=0,0
    if event.char == "a":
        x,y =-10,0
    if event.char == "d":
        x,y =10,0w
    if event.char == "w":
        x,y =0,-10
    if event.char == "s":
        x,y =-0,10
root.bind("",pressed)

root.mainloop()

Leave a Reply