不確定是否有人可以幫助我解決這個問題。我一直無法在任何地方找到一個簡單的答案。我正在 Kivy 中構建一個 GUI,它顯示網絡攝像頭提要(使用 openCV)并有兩個按鈕(按鈕 A 和 B)。當我按下按鈕 A 時,它會調用一個執行某些操作的函數。但是,當被調用的函數正在執行時,我的屏幕和 GUI 會凍結。如何實現按下按鈕調用的函數以在 python 中的不同線程上運行?
2 回答

慕斯709654
TA貢獻1840條經驗 獲得超5個贊
如果您的按鈕調用一個需要時間執行的函數,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
添加回答
舉報
0/150
提交
取消