Kivy Simple Multithreading python

痴心易碎 提交于 2019-12-23 04:02:29

问题


Not sure if anyone can help me with this. I have been unable to find a simple answer to this anywhere.

I am building a GUI in Kivy which shows the webcam feed (using openCV) and has two buttons (Button A and B). When I press Button A, it calls a function which does something. However, my screen and GUI freezes as the called function is executing.

How to I implement the function called by the button press to run on a different thread in python?


回答1:


If your button calls a function which takes time to execute, kivy window freezes till the function is done. You can use multi threading and have a thread execute that function. I don't have your code but for example:

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



回答2:


In your .kv file you can do:

#:import threading threading
.
.
.
    Button:
        on_release: threading.Thread(target=root.do_something).start()

see threading.



来源:https://stackoverflow.com/questions/52154496/kivy-simple-multithreading-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!