Pyttsx3: Thread not reading all the elements in my dictionary. The app quits after reading the first key

34 views Asked by At

I am trying to use pyttsx3 to read some elements in a dictionary. But because of runAndWait(), I need to use thread so that the user interface won't be blocked. The thread works in that sense, but the app quits right after reading the first element in the dictionary and says raise RuntimeError('run loop already started') RuntimeError: run loop already started

Her is the on_start function in the main class

    def on_start(self):
        self.load_tasks_from_file()
        self.view_tasks(allTasks)
        threading.Thread(target=self.read_me).start()

And here is the implementation of read_me()

    def read_me(self):
        global task_being_read
        if self.reading_in_progress:  # Check if reading is ongoing
            return  # Don't start new reading if one is in progress

        self.reading_in_progress = True  # Set flag to indicate reading is ongoing
        self.rate = self.engine.getProperty('rate')
        self.engine.setProperty('rate', 80)
        for key in allTasks:
            task_being_read = key
            print("The task being read is ", task_being_read)
            self.engine.say("Task" + str(key))
            self.engine.runAndWait()
        self.engine.stop()
        self.reading_in_progress = False  # Reset flag after reading finishes
0

There are 0 answers