How do I fix "name is not defined error" (tkinter)

Question:

I’m trying to make a stopwatch app with Tkinter, but now I have some trouble. Somewhy, I’m receiving the following error:

name 'label13' is not defined. line 177, in countdown
 label13.config(text = count)

I have no idea why this error message pops up. I really appreciate it if you can help me.

Here is my code:

from tkinter import Tk, Canvas, Entry, Text, Button, PhotoImage
from tkinter import *

window = Tk()
window.geometry("703x981")
window.configure(bg = "#FFFFFF")


def countdown(count):

    #change text in label        
    label13.config(text = count)

    if count > 0:
        #call countdown again after 1000ms (1s)
        window.after(1000, countdown, count-1)


countdown(120)


label13 = Label(window, font = "Courier 40 bold", bg="white", fg="black")
label13.place(x =29, y=300)


window.resizable(False, False)
window.mainloop()

Answers:

Because label13 should defined before calling countdown(120) as shown below.

from tkinter import Tk, Canvas, Entry, Text, Button, PhotoImage
from tkinter import *

window = Tk()
window.geometry("703x981")
window.configure(bg = "#FFFFFF")
window.resizable(False, False)

label13 = Label(window, font = "Courier 40 bold", bg="white", fg="black")
label13.place(x =29, y=300)

def countdown(count):
    #change text in label        
    label13.config(text = count)
    if count > 0:
        #call countdown again after 1000ms (1s)
        window.after(1000, countdown, count-1)


countdown(120)

window.mainloop()
Answered By: Erastus Nzula
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.