python - 当 pyttsx3 开始运行时,tkinter GUI 冻结
问题描述
我最近开始学习 pyttsx3 ,您可以在其中将文本转换为语音。我面临一个问题,一旦 pyttsx3 开始说话,我的整个 GUI 就会冻结,直到 pyttsx3 停止说话。
这是代码:
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()
在这里,当我单击大声朗读按钮时,整个 GUI 冻结,我无法对其进行任何操作,直到 pyttsx3 停止说话。
有没有办法解决这个问题?
如果有人可以帮助我,那就太好了。
解决方案
使用线程执行read()
:
import threading
...
read_button = Button(root, text="Read aloud", command=lambda: threading.Thread(target=read, daemon=True).start())
...
推荐阅读
- python - Python:如何获取 Active Directory 组成员的电子邮件?
- javascript - 两个 Appium 选择器,其中只有一个接受文本
- java - Aptana Studio 3 不会启动。它只看到 java 1.6 而不是 1.8
- .net - 如何在vb dot net中显示excel文件的所有线程或实例。我想显示excel文件的所有线程和一个主进程
- python - 如何在加载模型并使用一个类进行训练时对类进行编码器转换
- angular - 排序图标不能包含在 mdbTableSort 中
- microsoft-edge - 将搜索引擎从 Chrome 导入 Microsoft Chromium Edge
- python - python - 为不同的多类分类器绘制精确召回曲线
- python - Apache-Drill 不理解 Pandas datetime64[ns]
- flutter - 为什么 Visual Studio Code 会随机丢失 Flutter 类导入?