【发布时间】:2021-03-18 19:32:39
【问题描述】:
我正在尝试运行一个代码,该代码使用基于时间触发的相同功能来显示照片而不是主照片。 第一次调用该函数并返回主照片时它工作正常。但是当它第二次调用该函数时照片发生了变化,我看不到倒计时并且程序冻结并在终端字段中显示错误。
from tkinter import *
import time
root=Tk()
################uper frame for Day---Date----Time#####
frameup=Frame(root,highlightthickness=4,relief='solid',width=1000, height=60)
frameup.grid(row=0,column=0,columnspan=2,sticky='nsew',padx=250,pady=5)
frameup.config(highlightbackground='red3')
################lower frame############
framelow=Frame(root,highlightthickness=4,relief='solid',width=800, height=240)
framelow.grid(row=1,column=0,padx=210)
framelow.config(highlightbackground='red3')
################frame Left for the photo#############
frameL=Frame(framelow,width=380,height=20,relief='ridge',highlightthickness=1)
frameL.grid(row=0,column=0,padx=5)
frameL.config()
###############frame Right for Time Duration Countdown########
frameR=Frame(framelow)
frameR.grid(row=0,column=1,padx=5)
frameR.config(highlightbackground='green')
durationtext=Label(frameR,text='Time Remaining:',font='infra 40 bold')
durationtext.grid(row=0,column=0)
labelTD=Label(frameup)
labelTD.grid(row=0,column=0)
def countdown(n):
mn, secs =divmod(n, 60)
hr, mn =divmod(mn, 60)
labelCD.config(text=f"{hr:02}:{mn:02}:{secs:02}")
labelCD.config(font='infra 50 bold',foreground='black',background='white')
if n >= 0:
labelCD.after(1000, countdown, n-1)
else:
EYE1.forget()
labelCD.forget()
labelCD.destroy()
StudioC = Label(frameL, image=photoGenral)
StudioC.grid(row=0, column=0)
def clock():
t=time.strftime('%A''\t''%D''\t''%H:%M:%S',time.localtime()).upper()
if t!='':
labelTD.config(text=t,font='infra 50 bold',foreground='red3',background='white')
Hr = time.strftime('%H')
Mn = time.strftime('%M')
Sc = time.strftime('%S')
if int(Hr)==7 and int(Mn)==54 and int(Sc)==0: ####just to trigger the timer###
StudioC.forget()
EYE1 = Label(frameL, image=photo1)
EYE1.grid(row=0, column=0)
#######this condition works fine
countdown(100)
if int(Hr)==7 and int(Mn)==57 and int(Sc)==0:
EYE1 = Label(frameL, image=photo1)
EYE1.grid(row=0, column=0)
###this condition i can see the photo but I can't see countdown function and window froze
countdown(100)
if int(Hr)==7 and int(Mn)==59 and int(Sc)==0:
StudioC.destroy()
EYE1 = Label(frameL, image=photo1)
EYE1.grid(row=0, column=0)
countdown(100)
labelTD.after(1000,clock)
labelCD = Label(frameR)
labelCD.grid(row=1,column=0)
photo1 = PhotoImage(file="file path.png")
EYE1 = Label(frameL, image=photo1)
#
photoGenral = PhotoImage(file="file path.png")
StudioC = Label(frameL, image=photoGenral)
StudioC.grid(row=0,column=0)
clock()
root.geometry('1775x395')
root.mainloop()
【问题讨论】:
-
终端窗口显示什么错误?
-
@scotty3785 它的错误不止一行,但这两行具体是倒计时功能中的第三行和窗口冻结时的第二个 If 条件。请复制过去并添加两张照片以确切了解我在说什么。
-
请用完整的错误代码更新问题。
-
Tkinter 回调 Traceback 中的异常(最近一次调用最后一次):文件“//__init__.py”,第 1885 行,在 call__return self.func(*args) 文件“//__init.py",第 806 行,在 callit func(*args) 文件“/”,第 78 行,时钟倒计时 (100) 文件“/”,第 38 行,倒计时 labelCD.config(text=f"{ hr:02}:{mn:02}:{secs:02}") File "//__init__.py", line 1639, in configure return self._configure('configure',cnf, kw) File "//__init__ .py”,第 1629 行,在 _configure self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))_tkinter.TclError:无效命令名称“.!frame2.!frame2.!label2 "
-
我不得不删除一些显示路径的文本以正确计数。谢谢!
标签: python function tkinter time