tkinter时钟函数错误:NameE

2024-05-19 07:23:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我在tkinter中有一个时钟标签的函数:

def update_clock():
    global clock
    now = time.strftime("%H:%M:%S")
    clock.configure(text=now)
    clock.after(1000, update_clock)

我得到这个错误:

NameError: name 'update_clock' is not defined

在我读过的所有时钟教程中,最后一行的1000后面都有函数名。如何修复此错误?你知道吗


Tags: 函数texttimetkinterconfiguredef错误update
1条回答
网友
1楼 · 发布于 2024-05-19 07:23:51

由于您对这个小示例进行了postminimal编码以帮助您解决问题,因此错误告诉您在创建小部件之前正在调用函数。你知道吗

import time
from tkinter import *


def update_clock():
    now = time.strftime("%H:%M:%S")
    clock.configure(text=now)
    clock.after(1000, update_clock)

root = Tk()

clock = Label(root, bg="red")
clock.pack()

update_clock() # make sure you call this after you label has been packed

root.mainloop()

相关问题 更多 >

    热门问题