我正在做一个语音识别项目,并试图使用Tkinter为我的项目创建GUI ... SR部分工作正常,但是当我将其与Tkinter集成时却无法工作...请帮助。 (这是编程的新手,所以请不要介意我的代码:))
#MY CODE
import speech_recognition as sr
import tkinter as tk
obj = tk.Tk()
obj.title("SpeechToText")
obj.geometry('400x200')
obj.resizable(0,0)
def rec():
r = sr.Recognizer()
msg.configure(text="Say something")
while True:
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
audio = r.listen(source)
try:
txt = "".format(r.recognize_google(audio).get())
msg.configure(text=txt)
except Exception as e:
print(e)
break
msg = tk.Label()
msg.grid(row=0,column=0)
btn = tk.Button(text="Start",command=rec)
btn.grid(row=2,column=0)
obj.mainloop()
我希望它在标签中显示翻译后的文本,但不显示。即使说话后也只会显示“说些什么”。
答案 0 :(得分:1)
尝试一下,我屏蔽了msg.configure(text='Say Somethin')
。此行使记录的文本重新格式化为“说些什么”,而不是重新格式化为记录的文本。希望这会有所帮助。
import speech_recognition as sr
import tkinter as tk
obj = tk.Tk()
obj.title("SpeechToText")
obj.geometry('400x200')
obj.resizable(0,0)
def rec():
r = sr.Recognizer()
#msg.configure(text="Say something")
while True:
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source)
audio = r.listen(source)
try:
txt = r.recognize_google(audio)
msg.configure(text=txt)
print(txt)
except Exception as e:
print(e)
break
msg = tk.Label()
msg.grid(row=0,column=0)
btn = tk.Button(text="Start",command=rec)
btn.grid(row=2,column=0)
obj.mainloop()