簡體   English   中英

為什么我的 tkinter 代碼沒有在我的 ttsx3 代碼之前執行

[英]Why my tkinter code is not executing before my ttsx3 code

這是我的代碼:

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 窗口。 任何人都可以幫助我嗎?

首先,設置所有標簽,然后在wishme()函數中調用say() wishme()函數。 win.after(100,wishme)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

如果我交換

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM