I have made a simple timer program and a box layout with a label and a button in Kivy. Whenever the button is pressed, I want the timer to start and the label to display the time passed. I followed this tutorial: https://www.youtube.com/watch?v=cggCobcS3vU and then added some of my own stuff to the code, but the label only displays the initial text set in the .kv file. Any fix?
timer.py
import time
from kivy.app import App
from kivy.uix.label import Label
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.properties import StringProperty, ObjectProperty
class MyApp(BoxLayout):
def __init__(self, **kwargs):
super(MyApp, self).__init__(**kwargs)
self.output = ''
def update_label(self):
self.lbl1.text = str(self.output)
def count(self, *varargs):
timeLoop = True
Sec = 0
Min = 0
Hrs = 0
while timeLoop == True:
self.update_label()
print(str(self.output))
Sec += 1
time.sleep(1)
if Sec == 60:
Sec = 0
Min += 1
if Min == 60:
Min = 0
Hrs += 1
if Sec <= 9 and Min <=9 and Hrs <= 9:
self.output = '0' + str(Hrs) +'.'+ '0' + str(Min) + "." + '0' + str(Sec)
elif Sec <= 9 and Min <=9 and Hrs >=9:
self.output = str(Hrs) + '.'+ '0' + str(Min) + "." + '0' + str(Sec)
elif Sec <= 9 and Min >=9 and Hrs >=9:
self.output = str(Hrs) + '.'+ str(Min) + "." + '0' + str(Sec)
elif Sec >= 9 and Min >=9 and Hrs >=9:
self.output = str(Hrs) + '.'+ str(Min) + "." + str(Sec)
elif Sec >= 9 and Min >=9 and Hrs <=9:
self.output = '0' + str(Hrs) +'.'+ str(Min) + "." + str(Sec)
elif Sec >= 9 and Min <= 9 and Hrs <=9:
self.output = '0' + str(Hrs) +'.'+ '0' + str(Min) + "." + str(Sec)
elif Sec >= 9 and Min <=9 and Hrs >= 9:
self.output = str(Hrs) +'.'+ '0' + str(Min) + "." + str(Sec)
elif Sec <= 9 and Min >= 9 and Hrs <=9:
self.output = '0' + str(Hrs) +'.'+ str(Min) + "." + '0' + str(Sec)
class MainApp(App):
def build(self):
c = MyApp()
return c
if __name__ == '__main__':
MainApp().run()
mainapp.kv
<MyApp>:
lbl1: label1
BoxLayout:
size: root.size
Button:
id: button1
text: "Change text"
on_press: root.count()
Label:
id: label1
text: "hi"
I am aware of this: How to change text of a label in the kivy language with python
and this: https://groups.google.com/forum/#!topic/kivy-users/mdqPQYBWEU8
But neither seems to work for me.