我该如何解决这个问题,我希望语音识别将文本输入到我名为entry_1的Entrybox中,当我不使用gui但在添加tkinter后不显示任何内容时,代码可以正常工作。
from tkinter import *
import wikipedia
import wolframalpha
import speech_recognition as sr
import pyaudio
import pyspeech
class gui:
#gui
def __init__(self):
frame=Frame(root,width=600,height=0)
frame.pack()
self.label_1 = Label(root, text="Ask your Question:")
self.entry_1 = Entry(root)
self.entry_1.bind("<Return>",self.virtual)
self.label_1.pack(side=LEFT,pady=20)
self.entry_1.pack(side=LEFT,fill=X,pady=20,padx=10,expand=TRUE)
self.entry_1.focus_set()
def virtual(self,event):
value = self.entry_1.get()
value=value.lower()
if value=='': #checking if input is null
r = sr.Recognizer()
with sr.Microphone() as source:
audio = r.listen(source)
try:
self.entry_1.insert(r.recognize_google(audio))
except sr.UnknownValueError:
print("google speech could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
else:
try:
app_id = "some api" #enter you api
client = wolframalpha.Client(app_id)
res = client.query(value)
answers = next(res.results).text
print(answers)
except:
print(wikipedia.summary(value,sentences=2))
root=Tk()
root.wm_title("Hi,i am Karen your personal assistant") #title for gui
b=gui()
root.mainloop()