tkinter GUI freezes when pyttsx3 starts running

Lenovo 360

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.

acw1668

Use thread to execute read():

import threading

...

read_button = Button(root, text="Read aloud", command=lambda: threading.Thread(target=read, daemon=True).start())
...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TOP Ranking

HotTag

Archive