0

这是我的代码:

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?",font=("calibri",18),bg="black",fg="light green").grid(column=0,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, 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?",font=("calibri",15),bg="black",fg="light green").grid(column=0,row=2,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, 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?",font=("calibri",18),bg="black",fg="light green").grid(column=0,row=2,sticky=W)
        say("Good evening,sir.")
        say("what can I do for you today?")
    else:
        t1=Label(win,text="Hello, 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?",font=("calibri",18),bg="black",fg="light green").grid(column=0,row=2,sticky=W)
        say("Hello,sir.")
        say("what can I do for you today?")

wishme()

win.mainloop()

我要做的是先打开我的 tkinter 窗口,然后祝我早安或晚上等。但它是先祝我然后打开 tkinter 窗口。任何人都可以帮我吗?

4

2 回答 2

0

首先,设置所有标签,然后在say()函数中对wishme()函数的所有调用。tkinterwin.after(100,wishme)wishme()在 100 毫秒后分别运行到主循环。

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()
于 2020-09-28T13:43:55.923 回答
0

它说:

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

如果我交换

于 2020-09-28T14:00:58.127 回答