【问题标题】:Python Problem Using the While Loop and maybe Tkinter使用 While 循环和 Tkinter 的 Python 问题
【发布时间】:2020-04-08 20:47:58
【问题描述】:

我正在尝试在 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?

我不知道我做错了什么。

【问题讨论】:

标签: python tkinter time while-loop


【解决方案1】:

tkinter 窗口中的画布从来不是pack/grid/place

tkinter 需要一个.mainloop() 调用来管理图形更新,但.mainloop() 是一个阻塞调用。

您可以使用.after(time, function) 在一定时间(以毫秒为单位)后设置函数调用。

试试这个:

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[7] # I think you wanted index 7 not 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(10, update)
tk.mainloop()

【讨论】:

    【解决方案2】:

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

    tk.mainloop()

    在最后。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-19
      • 2020-08-18
      • 1970-01-01
      相关资源
      最近更新 更多