Python在使用While循环和Tkinter时出现问题
我试图在Python上制作这个时钟,但是出了点问题。在我激活它之后,pythonshell基本上什么都不做。这是我的密码:Python在使用While循环和Tkinter时出现问题,python,tkinter,time,while-loop,Python,Tkinter,Time,While Loop,我试图在Python上制作这个时钟,但是出了点问题。在我激活它之后,pythonshell基本上什么都不做。这是我的密码: #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 i
#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窗口将不会出现 tk.mainloop() 最后
当你关闭应用程序时,你会收到一条消息,说明你的程序正在运行,因为你的while循环正在后台运行。在Tkinter中,Mainloop将确保你的应用程序将更新每个帧。我还建议你将“tk”重命名为“root”或“master”因为这些名称是最常用的名称。画布从未在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()
导入时间
从tkinter进口*
def update():
_timenow=time.localtime()
__年份=_timenow[0]
__月份=_timenow[1]
__day=\u timenow[2]
__week\u day=\u timenow[6]
__小时=_timenow[3]
__分钟=_timenow[4]
__秒=_timenow[5]
__year_day=_timenow[7]#我想你想要的是索引7而不是索引6
_年份=__年
_月=月[\u月-1]
_天=天
lday=str(_day)[-1]
_周日=周日
_小时=小时
_分钟
_秒=秒
_年\日=\年\日
yday=str(年日)[-1]
如果lday==“1”:
day=str(_day)+“st”
elif lday==“2”:
day=str(_day)+“nd”
elif lday==“3”:
day=str(_day)+“rd”
elif lday不在['1'、'2'、'3']中:
day=str(_day)+“th”
如果_hour==0:
小时='12'
apm=‘上午’
elif_小时<12小时:
小时=str(_小时)
apm=‘上午’
elif_小时==12:
小时=str(_小时)
apm='下午'
elif_小时>=13:
小时=str(小时-12)
apm='下午'
如果yday=='1':
年日=str(年日)+“st”
elif yday==“2”:
年日=str(年日)+“nd”
elif yday==“3”:
年日=str(年日)+“rd”
elif yday不在['1'、'2'、'3']中:
年日=str(年日)+“th”
canvas.itemconfig(year_text,text=f'Today是{year}中的{year_day}天)
canvas.itemconfig(month\u day\u text,text=f'{u month}{day}')
itemconfig(time_text,text=f'{hour}:{{minute}:{{second}'))
canvas.itemconfig(apm_text,text=f'{apm}')
tk.之后(1000,更新)
tk=tk()
画布=画布(tk,宽度=500,高度=500)
canvas.pack()
标题(“时钟”)
tk.可调整大小(0,0)
年份文本=画布。创建文本(20,15,文本='今天是未知的未知日子',字体=('Comic Sans MS',20),状态='正常')
月\日\文本=画布。创建\文本(430,15,文本='Unknown',字体=('Comic Sans MS',20),状态='normal')
时间\文本=画布。创建\文本(200230,文本='Unknown:Unknown:Unknown',字体=('Comic Sans MS',35),状态='normal')
apm_text=canvas.create_text(235300,text='Unknown',font=('Comic Sans MS',25),state='normal')
月份=[‘一月’、‘二月’、‘三月’、‘四月’、‘五月’、‘六月’、‘七月’、‘八月’、‘九月’、‘十月’、‘十一月’、‘十二月’]
周日=[“星期一”、“星期二”、“星期三”、“星期四”、“星期五”、“星期六”、“星期日”]
tk.之后(10,更新)
tk.mainloop()
Read
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()