Python Forum
[Tkinter] Noob question:Using pyttsx3 with tkinter causes program to stop and close, any ideas? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Tkinter] Noob question:Using pyttsx3 with tkinter causes program to stop and close, any ideas? (/thread-30932.html)



Noob question:Using pyttsx3 with tkinter causes program to stop and close, any ideas? - Osman_P - Nov-13-2020

I installed the pyttsx3 libary to convert text to speech. And it works really great, until I want to use it in combination with tkinter. What happens that the program just closes, without any errors or what so ever. Weird enough it goes through the codes after engine.runAndWait(). I checked that by putting a print command after every line which shows up in the terminal, but I it 'ignores' the widgets that are supposed to be built up in the window. Any ideas? I'm using MacOs Mojave 10.14.6. with Python 3.8.5.

Here is the partial code with the problem, you can obviously ignore the print commands in between. I just left it so it's visible that it goes through the code, but only executing the print commands and ignoring the rest. Again, it works fine without tkinter, and the program also works fine when I don't use the pyttsx3 library. Here's the whole code: https://www.codepile.net/pile/rezzJ7ex and the JSON file that goes with it: https://www.codepile.net/pile/ePlYvbZM

else:
        random.shuffle(WORDS)
        for widget in window.winfo_children():
            widget.destroy()
        WORD = WORDS[0]
        tk.Label (window, image=photo1, bg="black") .grid(row=0, column=0)
        word = tk.Label (window, text = WORD, bg="black", fg="white", font="none 20 bold")
        word.grid(row=1, column=0)
        window.update()
        engine = pyttsx3.init()
        nl_voice_id = "com.apple.speech.synthesis.voice.ellen"
        engine.setProperty('rate', 100)
        engine.setProperty('voice', nl_voice_id)
        engine.say(WORD)
        engine.runAndWait()
        #del engine <this did not work
        print("after runAndWait")
        window.after(3000)
        print("after after")
        word['text'] = ''
        print("after text")
        window.update()
        print("after update")
        ANSWER = tk.Entry(window, width=20, bg="white")
        print("after entry")
        ANSWER.grid(row=2, column=0)
        print("after ANSWER")
        tk.Button(window, text="Check", width=6, command=lambda: check(ANSWER,WORD)) .grid(row=3, column=0)
        print("after button")
        WORDS.remove(WORD)
        print("after remove")



RE: Noob question:Using pyttsx3 with tkinter causes program to stop and close, any ideas? - Larz60+ - Nov-14-2020

Please show all code, or at least enough of it so that the issue can be replicated.


RE: Noob question:Using pyttsx3 with tkinter causes program to stop and close, any ideas? - joe_momma - Nov-14-2020

Looks like this part in your code is blocking the mainloop in tkinter
Quote:engine.runAndWait()
you may need to use threading -> thread -> start, to separate the voice to text.
I would create a function which starts the voice to text. threading module


RE: Noob question:Using pyttsx3 with tkinter causes program to stop and close, any ideas? - Osman_P - Nov-14-2020

(Nov-14-2020, 01:56 AM)joe_momma Wrote: Looks like this part in your code is blocking the mainloop in tkinter
Quote:engine.runAndWait()
you may need to use threading -> thread -> start, to separate the voice to text.
I would create a function which starts the voice to text. threading module

Thanks! Just to make sure.. All the print commands after
Quote:engine.runAndWait()
are executed in the terminal. I also googled threading but I don't know how to use it the functions of threading. So, while threading.TIMEOUT_MAX seems like an option, I don't have an idea how to implement it (just started python a few weeks ago..) Do you have suggestions?


RE: Noob question:Using pyttsx3 with tkinter causes program to stop and close, any ideas? - Osman_P - Nov-14-2020

(Nov-14-2020, 01:33 AM)Larz60+ Wrote: Please show all code, or at least enough of it so that the issue can be replicated.

Hi thanks, I've added the links to codepile containing the whole code and the JSON file, so I don't clutter the post. It's also my first post here. Would you recommend to paste the whole code here? Do you maybe also have suggestions for the question? Just started python a few weeks ago and I'm really stuck. Thanks.