Tkinter窗口将在代码完成后更新



当我运行代码时,窗口将在代码完成后出现,如果我在开始时添加主循环,代码将不会运行,直到我关闭窗口。我希望每次在代码中添加标签变量时窗口都会更新。我搜索了多个文档,但他们似乎都给出了相同的答案,它没有工作。

import pyttsx3
import datetime
import wikipedia
import webbrowser
import os
import tkinter
import speech_recognition as sr
from notifypy import Notify
window = 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 query
def 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 projectspythonD botdrone_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()

为事件驱动的GUI工具包(如tkinter)编写的程序与标准Python脚本有很大不同。

一旦你创建了一个窗口,用小部件填充它,并初始化了全局数据,你需要启动mainloop。如果没有运行的mainloop,则无法与GUI交互。

本质上,你的程序由一堆从主循环调用的函数组成,以响应用户操作控件,或计时器过期

(为了简单起见,我故意省略了threading之类的复杂情况)

我不认为pyttsx3是用事件驱动的gui编写的。因此,我怀疑您将不得不在单独的线程或进程中运行它。线程和进程都有各自的优缺点。

如果你使用一个进程来运行pyttsx3,你必须使用进程间通信显式地将数据发送到GUI进程。另一方面,声音收集过程不能干扰GUI。您可以相对容易地将它从GUI中分离出来进行测试。

如果你使用线程,你可能会有响应性问题,因为在CPython中一次只有一个线程可以执行Python字节码。另一方面,将数据传输到GUI是微不足道的,因为两者位于相同的地址空间中。根据我的经验,如果满足两个条件,可以从第二个线程调用tkinter函数或方法;

  • 你正在使用Python 3和
  • tcltk是在支持线程的情况下构建的。

相关内容

  • 没有找到相关文章

最新更新