Issue
I am trying to Implement a clock, but for now I am having trouble updating the label. I tried using a print statement to see weather the function is being called, and it sure is. But the label isn't being updated? Why is this happening?
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.image import Image
from kivy.config import Config
from kivy.clock import Clock
from kivy.properties import StringProperty
from kivy.graphics.instructions import InstructionGroup
from kivy.graphics.context_instructions import Color
import random
time_int= 0
class watch(BoxLayout):
time_str=StringProperty("00000:000000")
def incr(self):
global time_int
time_int+=1
time_str="00:0"+str(time_int)
print 'called'
class TimeApp(App):
def build(self):
return watch()
if __name__=="__main__":
TimeApp().run()
the .kv file
<watch@BoxLayout>:
Label:
text:root.time_str
Button:
on_press:root.incr()
Solution
Change <watch@BoxLayout>
to <watch>
. This creates a rule for the widget class defined in the python code, whereas the former creates a whole new class definition that doesn't need a python code equivalent.
Also, you should name your widgets starting with an upper case letter. This isn't important to you right now, but kv language uses it to distinguish them from properties in some cases so it's a good habit to get into (and also a normal python convention).
Answered By - inclement
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.