简体   繁体   中英

Functions acting strange when run as thread in Kivy/Python

I am building an app that, when the user hits a 'run' button, generates a table of buttons.

Because this process takes a while, I want to add a popup or progress bar to alert the user that the function is running and not frozen. To do this I decided to create a popup and call my function using threading so that the screen will be updated when the function starts (as opposed to once it is done).

mythread = threading.Thread(target=run_function)
mythread.start()

The trouble is that when I call my function from the above code it works very strangely: the columns of my table are the wrong width, some of my buttons are arbitrarily empty, and others have the wrong fill color. To fix this, all I need to do is to remove the threading operation and simply call run_function()

Any idea why this is happening?

I am new to Python, so it is likely some dumb mistake, but I have no idea. What is different between a process running as a thread and its default operation?

Disclaimer: I haven't worked with Kivy.

Not every framework works well with multithreading.

But most of the GUI frameworks have an event loop which is responsible for managing user events (mouse clicks, keyboard) and queued drawing operations (widgets).

In your case if don't want the UI to be freezed, you should regularly give control to your framework's event loop.

I guess kivy.base.EventLoopBase.dispatch_input is what you need to call to show an added widget or to handle user events.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM