Re globals, they are okay for quick and dirty scripts, but should be avoided for big complicated things. They are hard to debug, easy to get confused (I.e globals which are not declared as global are readonly), and can have problems with other modules using the same names. At the very least, put everything into a class as instance variables to encapsulate them. Or, pass things in/out of functions.
Re timers.... If you have specific things that get updated at specific intervals, consider ui.delay loops(which are basically equivalent to threading.Timers). Basically, a function calls itself after a delay, in a new thread. While it is waiting, other threads run. You could have the delay be a fixed time, or dependent on some other condition. Just be careful if there are interactions between threads, i.e one thread is modifying values used by another, in which case you have to synchronize using locks or another threadsafe mechanism. Also, be sure to include a stopping condition such as on_screen, otherwise it will try to keep running even after errors occur.
See a very simple example below, which updates two labels at different rates, using two independent delay loops.
import ui,time
class stopwatch(ui.View):
def __init__(self):
self.frame=(0,0,768,768)
self.bg_color='white'
fast=ui.Label(frame=(10,10,200,50),name='fast')
slow=ui.Label(frame=(10,70,200,50),name='slow')
stop=ui.Button(frame=(150,150,80,80),bg_color='red',name='stop')
go=ui.Button(frame=(250,150,80,80),bg_color='green',name='go')
go.title='go'
stop.title='stop'
go.action=self.go
stop.action=self.stop
self.add_subview(fast)
self.add_subview(slow)
self.add_subview(stop)
self.add_subview(go)
self._stopped=True
def stop(self,sender):
self._stopped=True
def go(self,sender):
if self._stopped:
self._stopped=False
self.fast_loop()
self.slow_loop()
def fast_loop(self):
if self.on_screen and not self._stopped:
self['fast'].text='{:0.14}'.format(time.time())
r=self['fast'].bg_color[0]
self['fast'].bg_color=((r+1)%2,1,1)
ui.delay(self.fast_loop,0.1)
def slow_loop(self):
if self.on_screen and not self._stopped:
self['slow'].text='{:0.14}'.format(time.time())
r=self['slow'].bg_color[0]
self['slow'].bg_color=((r+1)%2,1,1)
ui.delay(self.slow_loop,1.0)
if __name__=='__main__':
v=stopwatch()
v.present()