当我运行代码时,窗口会在代码执行完毕后才出现,如果我在代码开头添加主循环,那么代码就不会运行,直到我关闭窗口。我希望每次在代码中添加标签变量时,窗口都能更新。我查阅了多个文档,但它们似乎都给出了相同的答案,且没有效果。
import pyttsx3import datetimeimport wikipediaimport webbrowserimport osimport tkinterimport speech_recognition as srfrom notifypy import Notifywindow = tkinter.Tk()window.title("GUI")window.geometry('500x500')engine = pyttsx3.init('sapi5')voices = engine.getProperty('voices')engine.setProperty('voice', voices[0].id)# print(voices)def speak(audio): engine.say(audio) engine.runAndWait()def wishme(): hour = int(datetime.datetime.now().hour) if hour>=0 and hour<=12: speak("good morning sir") lab1 = tkinter.Label(window,text="Good morning sir").pack() elif hour>=12 and hour<=18: speak("good afternoon sir") lab2 = tkinter.Label(window,text="Good afternoon sir").pack() elif hour>=18 and hour<=22: speak("good evening sir") lab3 = tkinter.Label(window,text="Good evening sir").pack() else: speak("good night sir") lab4 = tkinter.Label(window,text="Good night sir").pack() lab5 = tkinter.Label(window,text="I am D bot,how may I help you").pack() speak("I am D bot,how may I help you")def takecommand(): r = sr.Recognizer() with sr.Microphone() as sourse: lab6 = tkinter.Label(window,text="listning...").pack() r.pause_threshold = 1 audio = r.listen(sourse) try: lab7 = tkinter.Label(window,text="recognizing...").pack() query = r.recognize_google(audio,language='en-in') # print(query) lab8 = tkinter.Label(window,text=query).pack() except Exception as e: lab9 = tkinter.Label(window,text="Good morning").pack() lab10 = tkinter.Label(window,text="say that again please").pack() speak("say that again please") takecommand().lower return "none" return querydef wiki(): # if 'wikipedia' in query: lab11 = tkinter.Label(window,text="searching wikipedia").pack() speak('searching wikipedia...') results = wikipedia.summary(query, sentences=2) lab12 = tkinter.Label(window,text="according to wikipedia").pack() speak("according to wikipedia") # print(results) lab13 = tkinter.Label(window,text=results).pack() speak(results) lab14 = tkinter.Label(window,text="check the notification for more details").pack() speak('check the notification for more details') notification = Notify() notification.title = "check out this website for more details" notification.message = 'https://en.wikipedia.org/wiki/Main_Page' notification.icon='G:\code projects\python\D bot\drone_115355.ico' notification.application_name="D bot" notification.send()if __name__=="__main__": wishme() while True: # if 1: query = takecommand().lower() # query = "play music" if 'open youtube' in query: webbrowser.get(using=None).open_new_tab("https://youtube.com/")# C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe # elif "close" in query: # break elif 'open amazon' in query: webbrowser.get(using=None).open_new_tab("https://www.amazon.com/") elif 'open gmail' in query: webbrowser.get(using=None).open_new_tab("https://mail.google.com/mail/u/0/#inbox") elif 'open google' in query: google = "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe" elif 'open chrome' in query: google = "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe" os.startfile(google) elif 'open stack overflow' in query: webbrowser.get(using=None).open_new_tab("https://stackoverflow.com/") elif "what's the time" in query: strtime = datetime.datetime.now().strftime('%H:%M:%S') # print('the time is '+strtime) lab15 = tkinter.Label(window,text="Hello sir,nice to meet you,how may i help you"+strtime).pack() speak('the time is '+strtime)window.mainloop()
很抱歉,之前mainloop
没有在代码中显示出来。我已经编辑了代码。
回答:
你只需要在希望窗口更新时添加这两个函数即可
window.update_idletasks() window.update()