如何在保持 tkinter 页面打开的同时创建 while true 循环?

How do you create a while true loop while still keeping a tkinter page open?

我正在尝试创建一个显示时间的 tkinter 页面,它应该不断更新。我试过:

from tkinter.font import *
import time
def SetTime():
    global time_date
    time_date = time.strftime("%H:%M")
    InfoTime.set(time_date)
Main = Tk()
Main.geometry("1600x1200")
Main.title("Time")
FontStyle = Font(family = "Times New Roman", size = 48)
InfoTime = StringVar()
TitleText = Label(Main,textvariable = InfoTime,font = FontStyle).pack()
while True:
    SetTime()

但是,运行 While True: 行和 运行 SetTime() constantly 出于某种原因阻止 tkinter 页面(主)打开。这一直是我很多 tkinter 项目的问题。

请注意,我是 运行 python 3.7.2 处于空闲状态。 谢谢

应该这样做:

from tkinter import *
from tkinter.font import *
import time

Main = Tk()
Main.geometry("1600x1200")
Main.title("Time")

FontStyle = Font(family = "Times New Roman", size = 48)
TitleText = Label(Main, font = FontStyle)
TitleText.pack()

time_date1 = ''

def SetTime():
    global time_date1
    global TitleText

    # Gets current time
    time_date2 = time.strftime("%H:%M")
    # If time has changed, update it
    if time_date2 != time_date1:
        time_date1 = time_date2
        TitleText.config(text=time_date2)

    # Repeats function every 200 milliseconds 
    TitleText.after(200, SetTime)

SetTime()
Main.mainloop()

评论几乎说明了一切。我还清理并重新格式化了您的代码,使其看起来更漂亮。