Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 退出窗口时tkinter中出现标签错误_Python_Python 3.x_Tkinter_Label_Clock - Fatal编程技术网

Python 退出窗口时tkinter中出现标签错误

Python 退出窗口时tkinter中出现标签错误,python,python-3.x,tkinter,label,clock,Python,Python 3.x,Tkinter,Label,Clock,我正试图通过python tkinter制作一个数字时钟 import tkinter,time def exiter(): root.destroy() root=tkinter.Tk() root.title("Digital Clock") root.geometry("340x100") root.resizable(False,False) root.protocol("WM_DELETE_WINDOW",exit

我正试图通过python tkinter制作一个数字时钟

import tkinter,time

def exiter():
    root.destroy()

root=tkinter.Tk()
root.title("Digital Clock")
root.geometry("340x100")
root.resizable(False,False)
root.protocol("WM_DELETE_WINDOW",exiter)

def time_setter():
    hr=tkinter.Label(root,font=('k',60,'bold'),text=time.strftime("%H:%M:%S"))
    hr.grid(row=0,column=0)

    

while True:
    root.update()
    time_setter()
在我关闭窗口后,我得到一个错误

Traceback (most recent call last):
  File "c:\Users\Tanmay Daga\OneDrive\Documents\Programming\Clock-Tkinter\digital_clock.py", line 20, in <module>
    time_setter()
  File "c:\Users\Tanmay Daga\OneDrive\Documents\Programming\Clock-Tkinter\digital_clock.py", line 13, in time_setter
    hr=tkinter.Label(root,font=('k',60,'bold'),text=time.strftime("%H:%M:%S"))
  File "C:\Program Files\Python39-32\lib\tkinter\__init__.py", line 3145, in __init__
    Widget.__init__(self, master, 'label', cnf, kw)
  File "C:\Program Files\Python39-32\lib\tkinter\__init__.py", line 2569, in __init__
    self.tk.call(
_tkinter.TclError: can't invoke "label" command: application has been destroyed
回溯(最近一次呼叫最后一次):
文件“c:\Users\Tanmay Daga\OneDrive\Documents\Programming\Clock Tkinter\digital\u Clock.py”,第20行,in
时间设定器()
文件“c:\Users\Tanmay Daga\OneDrive\Documents\Programming\Clock Tkinter\digital\u Clock.py”,第13行,in time\u setter
hr=tkinter.Label(root,font=('k',60,'bold'),text=time.strftime(“%H:%M:%S”))
文件“C:\Program Files\Python39-32\lib\tkinter\\uuuuu init\uuuuu.py”,第3145行,在\uuuu init中__
小部件。_u初始化(自、主、标签、cnf、kw)
文件“C:\Program Files\Python39-32\lib\tkinter\\uuuuu init\uuuuu.py”,第2569行,在\uuuu init中__
自我呼叫(
_tkinter.TclError:无法调用“label”命令:应用程序已被销毁
当窗口关闭时,如何打破循环。
我怎么知道窗户什么时候关着

from tkinter import *
from time import strftime

def clock_tick():
    string = strftime('%H:%M:%S %p')
    lbl.config(text=string)#set the text
    lbl.after(1000, time)#updater 
    
root=Tk() 
lbl = Label(root, font=('calibri', 40, 'bold'),
            background='purple',
            foreground='white')
lbl.pack(anchor = 'center')
clock_tick() 
root.title("Digital Clock")
root.geometry("340x100")
root.resizable(False,False)
root.mainloop() #this is the loop you are looking for instead of the while loop
循环是错误的。对于tkinter,有一个内置循环更适合使用。如果使用root.mainloop()设置循环,使用X退出该循环将提供一个干净的退出。下面是一个很好的示例,说明您正在尝试执行的操作

由于
时间
是内置模块,最好不要将其用作函数名。你是对的,我从geeks for geeks中提取了大部分内容。我已将其更改为与内置模块不冲突。