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

Time:07-29

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()

CodePudding user response:

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()
  • Related