简体   繁体   中英

kivy: popup with progress bar

Is it possible to create a popup with a "moving" progressbar?

Here is an example of what I tried, but it'll only display the popup after the the loop is complete. I'd like to have it visible while doing it.

import time

from kivy.app import App

from kivy.uix.button import Button
from kivy.uix.progressbar import ProgressBar
from kivy.uix.popup import Popup

databases = range(5)

class testApp(App):

    def launch_popup(self, instance):
        print("Button pressed for popup: {0}".format(instance))
        import time 

        pb = ProgressBar() #100
        popup = Popup(title='Syncing Databases', content=pb, size_hint=(0.7, 0.3))
        popup.open()

        for i in databases:
            time.sleep(1) #simulate syncing database
            pb.value += 100/len(databases)
            print("Progressbar is on {0}%".format(pb.value))


    def build(self):
        btn = Button(text="Popup", on_press=self.launch_popup)
        return btn


testApp().run()

Or should I use threads/a custom widget, if so how would I implement that?

Many thanks already!

Your problem is that the for loop blocks not just the popup, but everything else in your app - it runs in the same thread as kivy's eventloop, so kivy can't do anything at all until it's finished.

You should instead run your for loop in a separate thread or break it up into smaller components that can be scheduled with Clock.schedule_once or Clock.schedule_interval. This will let kivy perform its normal tasks in between running the bits of your code.

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