time.sleep isn't really compatible with ui. Instead, use ui.delay like so:# coding: utf-8 import ui, time class Main(ui.View): def __init__(self): self.view_main = ui.load_view() self.lbl = self.view_main["lbl_time"] self.view_main.present("sheet") self.time = 4 self.view_main["btn_start"].action = self.change_label_time def change_label_time(self, sender): ui.delay(self.decrement, 1) def decrement(self): if self.time > 0: self.time -= 1 self.lbl.text = str(self.time) ui.delay(self.decrement, 1) Main()
You're also abusing a custom view class. There's no reason to use a custom view class here, it's effectively being used as a function (__init__ called). It works just as well to doimport ui, time view_main = ui.load_view() lbl = view_main["lbl_time"] view_main.present("sheet") time = 4 #the action is this because it takes a function, what this does is calls it with a delay. view_main["btn_start"].action = lambda sender: ui.delay(decrement, 1) #This is still here because ui.delay takes a function. def decrement(): global time if time > 0: time -= 1 lbl.text = str(time) ui.delay(decrement, 1)
There were other problems with your code, I don't know why did_load was called in your code or how it worked, the contents of did_load contained an error, it should've beenself.view_main["btn_start"].action = self.change_label_time
instead ofself["btn_start"].action = self.change_label_time
I don't know how your button action got set since did_load contained an error and was never called anyway. It'd've be great if you'd included the pyui file on GitHub.