想要创建2个按钮,一个开始语音识别,并保持打开状态,直到我单击按钮完成为止。问题是当我单击“开始”并且没有说停止时:
import tkinter as tk
import speech_recognition as sr
window = tk.Tk()
window.title("Voice to Text")
window.geometry("300x350")
def startvoice():
r = sr.Recognizer()
with sr.microphone() as source:
print("speak now")
audio = r.listen(source)
try:
startvoice.voice2text = r.recognize_google(audio)
print("you said:{}".format(startvoice.voice2text))
except:
print("Your voice not clear")
def finished():
text_field.focus()
text_field.delete('1.0',tk.END)
text_field.insert('1.0',startvoice.voice2text)
text_field = tk.Text(master=window,height=20,width=40)
text_field.grid(column=0,row=2)
button1 = tk.Button(text="Start",width=16,command=startvoice)
button1.grid(column=0,row=0)
button1 = tk.Button(text="finish",command=finished)
button1.grid(column=0,row=1)
window.mainloop()