如果后面有其他指令,Kivy不会添加小部件



我正在尝试构建一个助手,它将说话,同时具有基本的用户界面。我有以下代码

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_hat方法中,在我添加小部件后,它不会在屏幕上显示给我,但会启动说话指令。我认为我需要重新设计它,并让引擎使用interface_manager,但我还不太确定。

谢谢!

我在添加新的小部件后,只需用逻辑启动一个新线程就可以做到这一点。

其背后的逻辑是,通过在"on_rerelease"上使用按钮和方法,操作将在它们全部完成后执行。至少这是我的理解。通过在添加小部件后启动一个新的线程,我确保屏幕会显示我想要的小部件,并且说话逻辑也会启动。

最新更新