你好,伙计们,当我开始运行一些功能时,我的kivy应用程序有问题,程序停止响应,我试图使用线程来解决这个问题,但它不起作用。
这是小部件类:
class PyWidget(Widget):
stop = threading.Event()
def start_thread(self):
self.ids.mic.source = 'assets/open2.png'
self.ids.mic.reload()
time.sleep(1)
threading.Thread(target=self.start_listening).start();
@mainthread
def start_listening(self):
while True:
try:
time.sleep(1)
print('Listening.......')
voiceText = RecognizeVoice()
# time.sleep(1)
if 'hello' in voiceText and Talking(App, respone, RecognizeVoice):
return
# else: Talking(App, respone, RecognizeVoice)
time.sleep(1)
except Exception as e:
print(f'start_listening: {e}')RecognizeVoice函数启动麦克风并将用户声音发送到文本
def RecognizeVoice():
try:
with speechRec.Microphone() as sound:
voice = recognizer.listen(sound)
voiceText = recognizer.recognize_google(voice, language="en-US") #online
voiceText = voiceText.lower()
print(f'Input : {voiceText}')
return voiceText
except speechRec.UnknownValueError as e:
print(f'RecognizeVoice: {e}')
except speechRec.RequestError as e:
print(f'RecognizeVoice: {e}')
respone('Sorry, something went wrong.')# Text to speech
def respone(message):
AI.say(message)
AI.runAndWait()在我的GUI中,我有一个按钮,当我单击start_thread函数启动时,所有其他函数都跟着它,我希望我解释了所有的事情。谢谢你的帮助
发布于 2022-01-26 19:57:58
@mainthread装饰器在start_listening()方法上击败threading。把那个装潢师移开。
https://stackoverflow.com/questions/70868220
复制相似问题