问题描述
这是我的代码:
from tkinter import*
import pyttsx3
import datetime
win=Tk()
win.configure(background="black")
win.geometry("500x700")
win.resizable(width=False,height=False)
#-----------------------------------------------------------------------------------------------------------------------------------------------
sys=pyttsx3.init()
voice_id="HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Speech\\Voices\\Tokens\\TTS_MS_Cortana"
sys.setProperty("voice",voice_id)
def say(audio):
sys.say(audio)
sys.runAndWait()
return
def wishme():
hour = int(datetime.datetime.Now().hour)
if hour>=0 and hour<12:
t1=Label(win,text="Good Morning,sir",font=("calibri",25),bg="black",fg="light green").grid(column=0,row=0,sticky=W)
t2=Label(win,text="What can I do for you today?",18),row=2,sticky=W)
say("Good Morning,sir.")
say("what can I do for you today?")
elif hour>=12 and hour<18:
t1=Label(win,text="Good Afternoon,15),sticky=W)
say("Good Afternoon,sir.")
say("what can I do for you today?")
elif hour>=18 and hour<21:
t1=Label(win,text="Good Evening,sticky=W)
say("Good evening,sir.")
say("what can I do for you today?")
else:
t1=Label(win,text="Hello,sticky=W)
say("Hello,sir.")
say("what can I do for you today?")
wishme()
win.mainloop()
我想做的是先打开我的tkinter窗口,然后祝我早,晚等。但是我希望我先打开tkinter窗口。有人可以帮我吗?
解决方法
首先,设置所有标签,然后在say()
函数中进行对wishme()
函数的所有调用。 tkinter的win.after(100,wishme)
将在主循环100毫秒后运行wishme()
。
def wishme():
hour = int(datetime.datetime.now().hour)
if hour>=0 and hour<12:
say("Good Morning,sir.")
elif hour>=12 and hour<18:
say("Good Afternoon,sir.")
elif hour>=18 and hour<21:
say("Good evening,sir.")
else:
say("Hello,sir.")
say("what can I do for you today?")
win.after(100,wishme)
win.mainloop()
,
它是这样说的:
Traceback (most recent call last):
File "c:/Python37/win2.py",line 51,in <module>
wishme()
File "c:/Python37/win2.py",line 40,in wishme
t1=Label(win,text="Good Evening,sir",font=("calibri",25),bg="black",fg="light green").grid(column=0,row=0,sticky=W)
File "C:\Python37\lib\tkinter\__init__.py",line 2766,in __init__
Widget.__init__(self,master,'label',cnf,kw)
File "C:\Python37\lib\tkinter\__init__.py",line 2299,in __init__
(widgetName,self._w) + extra + self._options(cnf))
_tkinter.TclError: can't invoke "label" command: application has been destroyed
如果我交换了