使用While循环和Tkinter的Python问题

我正在尝试在Python上使用此时钟,但是出了点问题。激活它后,Python Shell基本上不执行任何操作。这是我的代码:

#Time

import time
from tkinter import *

tk = Tk()
canvas = Canvas(tk,width = 500,height = 500)
tk.title('Clock')
tk.resizable = (0,0)
year_text = canvas.create_text(20,15,text = 'Today is the unknown day in unkown',font = ('Comic Sans MS',20),state = 'normal')
month_day_text = canvas.create_text(430,15,text = 'Unknown',font = ('Comic Sans MS',20),state = 'normal')
time_text = canvas.create_text(200,230,text = 'Unknown:Unknown:Unknown',font = ('Comic Sans MS',35),state = 'normal')
apm_text = canvas.create_text(235,300,text = 'Unknown',font = ('Comic Sans MS',25),state = 'normal')
activate = False


months = ['January','February','March','April','May','June','July','August','September','October','November','December']
week_days = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']

activate = True


while activate == True:
    _timenow = time.localtime()
    __year = _timenow[0]
    __month = _timenow[1]
    __day = _timenow[2]
    __week_day = _timenow[6]
    __hour = _timenow[3]
    __minute = _timenow[4]
    __second = _timenow[5]
    __year_day = _timenow[6]


    _year = __year
    _month = months[__month - 1]
    _day = __day
    lday = str(_day)[-1]
    _week_day = week_days[__week_day]
    _hour = __hour
    _minute = __minute
    _second = __second
    _year_day = __year_day
    yday = str(_year_day)[-1]


    if lday == '1':
        day = str(_day) + 'st'

    elif lday == '2':
        day = str(_day) + 'nd'

    elif lday == '3':
        day = str(_day) + 'rd'

    elif lday not in ['1','2','3']:
        day = str(_day) + 'th'


    if _hour == 0:
        hour = '12'
        apm = 'A.M.'

    elif _hour < 12:
        hour = str(_hour)
        apm = 'A.M.'

    elif _hour == 12:
        hour = str(_hour)
        apm = 'P.M.'

    elif _hour >= 13:
        hour = str(_hour - 12)
        apm = 'P.M.'

    if yday == '1':
        year_day = str(_year_day) + 'st'

    elif yday == '2':
        year_day = str(_year_day) + 'nd'

    elif yday == '3':
        year_day = str(_year_day) + 'rd'

    elif yday not in ['1','2','3']:
        year_day = str(_year_day) + 'th'


    canvas.itemconfig(year_text,text = f'Today is the {year_day} day in {_year}')
    canvas.itemconfig(month_day_text,text = f'{_month} {day}')
    canvas.itemconfig(time_text,text = f'{hour}:{_minute}:{_second}')
    canvas.itemconfig(apm_text,text = f'{apm}')

虽然它没有给我tkinter画布。我认为它与while循环有关,因为当我关闭它时,它说:

Your program is still running!
Do you want to kill it?

我不知道我怎么了

评论
  • 兰Oo
    兰Oo 回复

    There needs to be a .mainloop() call for tkinter to manage graphical updating, but .mainloop() is a blocking call.

    You can use .after(time, function) to setup a function call after an amount of time in milliseconds.

    试试看:

    import time
    from tkinter import *
    
    def update():
        _timenow = time.localtime()
        __year = _timenow[0]
        __month = _timenow[1]
        __day = _timenow[2]
        __week_day = _timenow[6]
        __hour = _timenow[3]
        __minute = _timenow[4]
        __second = _timenow[5]
        __year_day = _timenow[6]
    
    
        _year = __year
        _month = months[__month - 1]
        _day = __day
        lday = str(_day)[-1]
        _week_day = week_days[__week_day]
        _hour = __hour
        _minute = __minute
        _second = __second
        _year_day = __year_day
        yday = str(_year_day)[-1]
    
    
        if lday == '1':
            day = str(_day) + 'st'
    
        elif lday == '2':
            day = str(_day) + 'nd'
    
        elif lday == '3':
            day = str(_day) + 'rd'
    
        elif lday not in ['1','2','3']:
            day = str(_day) + 'th'
    
    
        if _hour == 0:
            hour = '12'
            apm = 'A.M.'
    
        elif _hour < 12:
            hour = str(_hour)
            apm = 'A.M.'
    
        elif _hour == 12:
            hour = str(_hour)
            apm = 'P.M.'
    
        elif _hour >= 13:
            hour = str(_hour - 12)
            apm = 'P.M.'
    
        if yday == '1':
            year_day = str(_year_day) + 'st'
    
        elif yday == '2':
            year_day = str(_year_day) + 'nd'
    
        elif yday == '3':
            year_day = str(_year_day) + 'rd'
    
        elif yday not in ['1','2','3']:
            year_day = str(_year_day) + 'th'
    
    
        canvas.itemconfig(year_text,text = f'Today is the {year_day} day in {_year}')
        canvas.itemconfig(month_day_text,text = f'{_month} {day}')
        canvas.itemconfig(time_text,text = f'{hour}:{_minute}:{_second}')
        canvas.itemconfig(apm_text,text = f'{apm}')
        tk.after(1000, update)
    
    tk = Tk()
    canvas = Canvas(tk,width = 500,height = 500)
    canvas.pack()
    tk.title('Clock')
    tk.resizable(0,0)
    year_text = canvas.create_text(20,15,text = 'Today is the unknown day in unkown',font = ('Comic Sans MS',20),state = 'normal')
    month_day_text = canvas.create_text(430,15,text = 'Unknown',font = ('Comic Sans MS',20),state = 'normal')
    time_text = canvas.create_text(200,230,text = 'Unknown:Unknown:Unknown',font = ('Comic Sans MS',35),state = 'normal')
    apm_text = canvas.create_text(235,300,text = 'Unknown',font = ('Comic Sans MS',25),state = 'normal')
    
    months = ['January','February','March','April','May','June','July','August','September','October','November','December']
    week_days = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
    
    tk.after(1000, update)
    tk.mainloop()
    
  • cnon
    cnon 回复

    如果您不添加,Tkinter窗口将不会出现

    tk.mainloop

    在最后。

    当您关闭应用程序时,您会收到一条消息,表明您的程序正在运行,因为while循环在后台运行。在Tkinter Mainloop中,请确保您的应用程序将更新每个Frame。我也建议您将“ tk”重命名为“ root”或“ master”,因为这些名称是最常用的名称。