gpt4 book ai didi

python - 如何从 Kivy 按钮启动线程?

转载 作者:行者123 更新时间:2023-12-01 01:38:35 26 4
gpt4 key购买 nike

不确定是否有人可以帮助我解决这个问题。我无法在任何地方找到这个问题的简单答案。

我正在 Kivy 中构建一个 GUI,它显示网络摄像头源(使用 openCV)并有两个按钮(按钮 A 和 B)。当我按下按钮 A 时,它会调用一个执行某些操作的函数。然而,当被调用的函数正在执行时,我的屏幕和 GUI 卡住了。

如何实现按下按钮时调用的函数以在 python 中的不同线程上运行?

最佳答案

如果您的按钮调用一个需要时间执行的函数,kivy 窗口将卡住,直到该函数完成。您可以使用多线程并让一个线程执行该函数。我没有你的代码,但例如:

from threading import Thread

# the function that the button executes
def button_press():
# create the thread to invoke other_func with arguments (2, 5)
t = Thread(target=other_func, args=(2, 5))
# set daemon to true so the thread dies when app is closed
t.daemon = True
# start the thread
t.start()


def other_func(a, b):
# your code here

关于python - 如何从 Kivy 按钮启动线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52154496/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com