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

Question:

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!! :))

Asked By: nev nev

||

Answers:

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()
Answered By: Amari
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.