我正试图建立一个助理,它将发言,同时将有一个基本的用户界面。我有以下代码
class InterfaceManager(BoxLayout):
__ENGINE = Engine(speaker=AssistantSpeaker(),
recorder=VoiceRecorder())
def __init__(self, **kwargs):
super(InterfaceManager, self).__init__(**kwargs)
self.__initial_screen = Button(text="Click this screen to start using the virtual assistant.")
self.__initial_screen.bind(on_press=self._assistant_chat)
self.__assistant_talking = Label(text="The assistant is talking.")
self.__new_diagnosis_widget = Button(text="New diagnosis")
self.__new_diagnosis_widget.bind(on_press=self._new_diagnosis)
self.__context = Context()
self.add_widget(self.__initial_screen)
def __show_conversation(self):
self.clear_widgets()
self.__conversation = Label(text=self.__context.get_context())
self.add_widget(self.__conversation)
def __show_recommendations(self):
# TODO: make recommendations based on the context
print("No recommendations")
pass
def __new_diagnosis(self):
self.clear_widgets()
self.__context.clear_context()
self.add_widget(self.__new_diagnosis_widget)
def _assistant_chat(self, button):
self.clear_widgets()
self.add_widget(self.__assistant_talking)
self.__ENGINE.speak("Wait for about one or two seconds after each of my questions, then answer.")
for symptom in SymptomsPhrases:
self.__ENGINE.speak(symptom.value)
self.__ENGINE.record()
recorded_transcribe = self.__ENGINE.transcribe()
self.__context.add_assistant_phrase(symptom.value)
self.__context.add_user_phrase(recorded_transcribe)
self.__context.print_context()
self.__show_conversation()
self.__ENGINE.speak('I am going to make recommendations based on these answers. Do you want me to ask again?')
self.__ENGINE.record()
recorded_transcribe = self.__ENGINE.transcribe()
if 'no' in recorded_transcribe.lower():
self.__show_recommendations()
self.__new_diagnosis()
else:
self._assistant_chat(button=button)
self.__context.clear_context()
def _new_diagnosis(self, button):
self.clear_widgets()
self.add_widget(self.__initial_screen)我遇到的问题是,在_assistant_chat方法中,在我添加小部件之后,它不会在屏幕上向我显示它,但是发言的指令将开始。我相信我需要重新设计这个,让引擎使用interface_manager,但我还不太确定。
谢谢!
发布于 2020-04-15 20:32:09
我在添加了新小部件之后,简单地用逻辑启动了一个新线程,从而成功地完成了任务。
其背后的逻辑是,通过在"on_release“上使用按钮和方法,操作将在它们全部完成后执行。至少这是我的理解。通过在添加小部件之后启动一个新线程,我确保屏幕将显示我想要的小部件,并且将启动说话逻辑。
https://stackoverflow.com/questions/61163981
复制相似问题