Tkinter窗口会在代码执行完毕后更新

当我运行代码时,窗口会在代码执行完毕后才出现,如果我在代码开头添加主循环,那么代码就不会运行,直到我关闭窗口。我希望每次在代码中添加标签变量时,窗口都能更新。我查阅了多个文档,但它们似乎都给出了相同的答案,且没有效果。

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()

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注