Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

“name is not defined” inside function

I want to create a countdown clock and I almost did but if I run this code down below and press the 'go' button, I get the error: NameError: name 'be' is not defined. Even if I try to put global be in there it doesn't seem to work

import tkinter as tk
def set1():
    global be
    if be1 is not '':
        be = int(en.get())
    
def countdown():
    global be
    
    if be >= 0:
        mins, secs = divmod(be, 60)
        timer = '{:02d}:{:02d}'.format(mins, secs)
        
        label['text'] = timer
        root.after(1000, countdown)
        be -= 1

root = tk.Tk()

label = tk.Label(root, text = '00:00', font = 'Helvetica 25')
label.pack()

en = tk.Entry(root)
en.pack()
be1 = en.get()
tk.Button(root, text = 'set', height = 3, width = 20, command = lambda: set1()).pack()
tk.Button(root, text = 'go', height = 3, width = 20, command = lambda: countdown()).pack()

root.mainloop()
like image 291
red_panda Avatar asked May 25 '26 06:05

red_panda


1 Answers

Please give be a value on top of your code, for example

be = 0