I'm just now learning Kivy for some UI options when creating stuff with Python. The thing I'm strugling right now are loops and how to make them work with Kivy.
Example below is a very straightforward loop that should print each number to a Kivy label (replacing previous).
Currently when pressing the button I only get the last digit on screen because the entire function runs completely - which makes sense. But my question is how can I setup the button so that when I press it, it runs only one iteration of the loop.
My .py file:
from kivy.app import App
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.uix.widget import Widget
Window.size=(300,300)
Builder.load_file('loop.kv')
class MyLayout(Widget):
def loop(self):
for i in range(10):
self.ids.label_print.text = str(i)
pass
class LoopApp(App):
def build(self):
return MyLayout()
if __name__=='__main__':
LoopApp().run()
My .kv file:
<MyLayout>
BoxLayout:
orientation: 'vertical'
size: root.height, root.width
Label:
id: label_print
font_size: 32
Button:
text: 'Loop'
on_press: root.loop()
Currently it only shows 9 as a result and I would expect to go from 0 and add 1 for each button press: