python - 防止 pyttsx3 卡住 GUI

标签 python multithreading kivy

我已经构建了我的 Kivy GUI,它与 pyttsx3 一起用于语音输出,但是当我运行 pyttsx3 时它会阻塞主线程,从而导致 GUI 卡住。

我如何在另一个线程上运行 pyttsx3 并从主线程听到输出,或者有没有办法在不阻塞主线程的情况下运行 pyttsx3 并防止它卡住我的 Kivy GUI?

这是我创建的示例代码,当您单击按钮时,它应该从文本框中打印文本,但它会导致 GUI 在 pyttsx3 运行时卡住:

import pyttsx3
from kivy.uix.textinput import TextInput
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button
from kivy.app import App



engine = pyttsx3.init()

engine.setProperty('rate', 150)
engine.setProperty('volume', 1)

class example(App):

    def build(self):

        layout = FloatLayout()

        self.textbox = TextInput(text="", multiline=False, font_size=12, size_hint_y=None, size_hint_x=None, width = 150, height = 30, pos_hint= {"x":0.4, "y":0.8})



        self.btnText = Button(text="Print text", font_size=12, size_hint_y=None, size_hint_x=None, width = 150, height = 30, pos_hint= {"x":0.4, "y":0.6})
        self.btnText.bind(on_press=self.print)


        layout.add_widget(self.textbox)
        layout.add_widget(self.btnText)

        return layout

    def print(self, instance):
        engine.say("this is an example of kivy being blocked my pyttsx3")
        engine.runAndWait()
        print(self.textbox.text)



if __name__ == '__main__':
    example().run()

我尝试在下面这样做:

thread2 = threading.Thread(target=engine.say, args = ("some text here",))
thread2 = threading.Thread(target=engine.runAndWait(),)
thread2.start()
thread2.join()

但上面的代码仍然阻塞线程并导致 Kivy GUI 卡住。

最佳答案

正如他指出的那样,您必须在一个线程中运行它:

import threading

# ...

class example(App):
    # ...

    def print(self, instance):
        threading.Thread(
            target=self.run_pyttsx3, args=(self.textbox.text,), daemon=True
        ).start()

    def run_pyttsx3(self, text):
        engine.say(text)
        engine.runAndWait()

# ...

关于python - 防止 pyttsx3 卡住 GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58630010/

相关文章:

c# - 使用线程从两个长时间运行的方法返回值

python - Kivy 应用程序加载后崩溃

c# - WPF/C# 中这种线程方式效率低下吗?

python - 将时区感知日期时间列转换为具有 UTC 时间偏移量的字符串

python - Django 在类中排序代码

python - 对缺少 </td> 标签的 HTML 表格使用 Beautiful Soup

multithreading - 与启用线程的 Perl 相比,为什么非线程 Perl 不使用 off64_t 类型?

python - 弹出窗口中的 Kivy 按钮不调用函数

python - 尝试使用 Pyinstaller 从 .py 脚本创建 .exe 时出现运行时错误

Python:快速将 7 GB 文本文件加载到 unicode 字符串中