我必須將長任務分成執行緒。在 Kivy 應用程式中的任何位置創建執行緒都會使整個應用程式等待執行緒函式結束,因此使用與不使用執行緒沒有區別。我究竟做錯了什么?
kv檔案:
BoxLayout:
Button:
on_press: threading.Thread(target=app.test()).start()
Button:
on_press: app.press()
蟒蛇代碼:
class MyApp(App):
running = True
def on_stop(self):
self.running = False
def test(self):
while self.running:
print('test')
time.sleep(2)
def press(self):
print('press')
if __name__ == '__main__':
MyApp().run()
單擊按鈕并創建執行緒后,應用程式將凍結。如何創建后臺執行緒?
uj5u.com熱心網友回復:
Thread(target=app.test())app.test()立即呼叫,然后進入無限回圈。目標是函式本身,而不是它的回傳值。代碼應如下所示(不帶括號):
threading.Thread(target=app.test).start()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/382681.html
