gpt4 book ai didi

python - “name is not defined” 内部函数

转载 作者:行者123 更新时间:2023-12-04 03:35:09 26 4
gpt4 key购买 nike

我想创建一个倒计时时钟,我几乎做到了,但是如果我在下面运行这段代码并按下“开始”按钮,我会收到错误消息: NameError: name 'be' is not defined。即使我尝试将 global be 放在那里,它似乎也不起作用

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

最佳答案

请在您的代码之上给 be 一个值,例如

be = 0

关于python - “name is not defined” 内部函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67055962/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com