[我在这里创建了一个虚拟助手python语言输入图像描述,我想在我的tinter窗口中显示控制台输出。]我试着这么做很多次,但我不能做到这一点。我使用了python的tkinter函数标签,但它没有在tkinter窗口上给我任何类型的输出,所以请帮助我获得理想的输出。在这里输入图像描述
from cProfile import label
import tkinter as tk
from telnetlib import SE
from tkinter import BOTTOM, LEFT, NW, RIGHT, SW, Button, Label, StringVar, Tk
from PIL import Image, ImageTk
from email.mime import audio
from logging import exception
from unittest import result
import pyttsx3
import speech_recognition as sr
import datetime
import wikipedia
import webbrowser
import os
def Voice_Assistant():
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
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")
elif hour>=12 and hour<18:
speak("Good Afternoon sir")
else:
speak("Good evening sir")
speak("I am Zaara, Please tell me how may i help you")
def takeCommand():
# It takes microphone input form the user and returns string output
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening....")
l1=Label(window, text="Listening....").pack()
r.pause_threshold = 0.5
r.energy_threshold = 2000
audio = r.listen(source)
try:
print("Recognizing...")
r1=Label(window, text="Recognizing...", justify='left', background='light blue').grid(row=9, column=6)
query = r.recognize_google(audio, language='en-in')
print(f"User said : {query}n")
r1=Label(window, text=f"User said : {query}n", justify='left', background='light blue').grid(row=9, column=6)
except Exception as e:
# print(e)
print("Say it again please...")
r2=Label(window, text="Say it again please...", justify='left', background='light blue').grid(row=9, column=6)
return "None"
return query
if __name__ == "__main__":
wishme()
while True:
query = takeCommand().lower()
# Logic for executing tasks based on query
if 'wikipedia' in query:
speak('searching wikipedia....')
query = query.replace("wikipedia", "")
results = wikipedia.summary(query, sentences=2)
speak("According to wikipedia")
print(results)
speak(results)
elif 'open youtube' in query:
webbrowser.open("youtube.com")
speak("ok! i'll open Youtube for you")
elif 'open google' in query:
webbrowser.open("google.com")
speak("ok! i'll open google for you")
elif 'stack overflow' in query:
webbrowser.open("stackoverflow.com")
speak("ok! i'll open stack overflow for you")
elif 'play music' in query:
music_dir = 'D:\Downloads\music01'
songs = os.listdir(music_dir)
# print(songs)
os.startfile(os.path.join(music_dir, songs[13]))
speak("ok i'll play music for you")
elif 'what is time now' in query:
strTime = datetime.datetime.now().strftime("%H: %M: %S")
speak(f"sir, the time is {strTime}")
elif 'open powerpoint' in query:
codepath = "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\PowerPoint 2016.lnk"
os.startfile(codepath)
speak("ok! i'll open power point for you")
elif 'open chrome' in query:
codepath = "C:Program FilesGoogleChromeApplicationchrome.exe"
os.startfile(codepath)
speak("ok! i'll open google chrome for you")
elif 'quit' in query:
speak("ok! i'll quit your program")
exit()
elif 'open my university website' in query:
webbrowser.open("gbu.ac.in")
speak("ok! i'll open your university website for you")
elif 'open control panel' in query:
codepath = "C:\Users\ik597\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\System Tools\Control Panel.lnk"
os.startfile(codepath)
speak("ok! i'll open control panel for you")
elif 'open ms word' in query:
codepath = "C:\Users\ik597\OneDrive\Attachments\Desktop\Word 2016.lnk"
os.startfile(codepath)
speak("ok! i'll open microsoft word for you")
elif 'open monkey type' in query:
webbrowser.open("Monkeytype.com")
speak("ok! i'll open monkeytype for you")
elif 'open passport india portal' in query:
webbrowser.open("passportindia.org.in")
speak("ok! i'll open passport india portal for you")
# Talk to zara
elif 'what is your name' in query:
speak("My name is zaara")
elif 'open This pc' in query:
speak("I can't open")
elif 'how are you' in query:
speak('I am fine, thanks. How about yourself')
elif 'can you be my friend' in query:
speak('Of course, I am your VFF virtual friend forever')
window =tk.Tk()
window_width=680
window_height=500
window.minsize(680,500)
window.maxsize(680,500)
screen_width=window.winfo_screenwidth()
screen_height=window.winfo_screenheight()
center_x = int(screen_width/2 - window_width / 2)
center_y = int(screen_height/2 - window_height / 2)
window.geometry(f'{window_width}x{window_height}+{center_x}+{center_y}')
window.configure(bg='light blue')
img = Image.open('voice1.png')
img1=Image.open('button.png')
window.iconbitmap("V logo.ico")
window.title("Zara- The Virtual Assistant")
photo = ImageTk.PhotoImage(img)
button= Button(window, image=photo, borderwidth=0, cursor="hand2", background='light blue', command=Voice_Assistant)
button.place(x=315,y=300)
inputtxt = tk.Text(window,height = 2,width = 70)
inputtxt.place(x=25,y=450)
photo1 = ImageTk.PhotoImage(img1)
button= Button(window, image=photo1, borderwidth=0, cursor="hand2", background='light blue')
button.place(x=602,y=443)
window.mainloop()
引起我注意的问题是您使用了"while "循环。您的程序将启动窗口和所有内容,然后进入连续的"while "循环。因此,从那时起,Tkinter将不再刷新。这就是为什么Windows会告诉你窗口没有响应的原因。因为它没有刷新,所以您不能动态更改任何内容。唯一的解决方案,我知道,将是使用多线程。你基本上需要把你的语音助手放在一个类中,然后使用Python多线程让它在另一个线程上运行。然后,您应该能够向包含您想要显示的文本的窗口添加一个标签。有关python线程的教程,您可以查看此站点:https://realpython.com/intro-to-python-threading/