简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM