Vaduva Mihaita Bogdan
Vaduva Mihaita Bogdan

Reputation: 170

Kivy not adding widget if another instruction is after it

I am trying to build an assistant that will speak and at the same time will have a basic user interface. I have the following code

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)

The problem I am having is that in the _assistant_chat method, after I add the widget, it won't display it to me on the screen, but the instruction to speak will start. I believe I need to redesign this and have the engine use the interface_manager, but I am not quite sure yet.

Thank you!

Upvotes: 1

Views: 40

Answers (1)

Vaduva Mihaita Bogdan
Vaduva Mihaita Bogdan

Reputation: 170

I managed to do it by simply starting a new thread with the logic after I added the new widget.

The logic behind it is that by using a button and a method on "on_release" was that the action would execute after they were all finished. At least this was my understanding. By starting a new thread after adding the widget, I made sure that the screen would show my widget that I want and also, the speaking logic will start.

Upvotes: 1

Related Questions