Reputation: 647
I recently started learning about pyttsx3 , where you can convert text to speech. I'm facing a problem where as soon as pyttsx3 starts speaking , my whole GUI freezes until pyttsx3 stops speaking.
Here's the code:
from tkinter import *
import pyttsx3
root = Tk()
def read():
engine.say(text.get(1.0 , END))
engine.runAndWait()
engine = pyttsx3.init()
text = Text(width = 65 , height = 20 , font = "consolas 14")
text.pack()
text.insert(END , "This is a text widget\n"*10)
read_button = Button(root , text = "Read aloud" , command = read)
read_button.pack(pady = 20)
mainloop()
Here , when I click on the read aloud button, the entire GUI freezes and I can't do anything with it until pyttsx3 stops speaking.
Is there any way to fix this problem ?
It would be great if anyone could help me out.
Upvotes: 0
Views: 408
Reputation: 46687
Use thread to execute read()
:
import threading
...
read_button = Button(root, text="Read aloud", command=lambda: threading.Thread(target=read, daemon=True).start())
...
Upvotes: 2
Reputation: 1
use thread u can check my GitHub demo code for this
https://github.com/rkb9878/KBC/blob/master/kbc_question.py
line no 325
Upvotes: 0