pythonkivy

Kivy: Using progress bar to simulate a process and then execute other code


I'm trying to use the progress bar in Kivy to simulate completion of an external process, at the end of which a function should run to provide some results.

I've looked at all the posts here discussing progress bar and Clock scheduled events. I've tried the methods, but I still can't get the effect I want.

Within my main widget class, I have the following methods defined:

def update_progbar(self):
    print(self.progbar.value)
    self.progbar.value += 10.0
def start_progbar_update(self):
    print("start progbar update")
    self.update_progbar_event = Clock.schedule_interval(lambda dt: self.update_progbar(),1)
def stop_progbar_update(self):
    print("stop progbar update")
    self.update_progbar_event.cancel()
def run_program(self):
    if self.progbar.value < 100.0:
       self.start_progbar_update()
    elif self.progbar.value >= 100.0:
       self.stop_progbar_update()

#wait for progress bar to fill up, then execute code below
    results = some_function(args)

So, the effect is to display to the user a simulated process that takes some time, and then produces some results.

The starting of progress bar update is working. I see the progress bar widget fill up 10% every second. But after it gets to 100%, it keeps running and keeps printing "100.0" and never exits.

I can't seem to get the condition if progbar.value >= 100.0: stop_progbar_update() to work. The Clock schedule interval event never gets cancelled.

The code below it never gets a chance to run.


Solution

  • You need to add code in your update_progbar() method to check for the end condition. Something like:

    def update_progbar(self):
        print(self.progbar.value)
        self.progbar.value += 10.0
        if self.progbar.value >= 100.0:
            self.stop_progbar_update()
            print('run some program')
    

    I suspect that your run_program() method is only called once at the start of your App, so that the code:

    elif self.progbar.value >= 100.0:
    

    is never run.