无法更新文本标签

Trouble updating text Label

我正在尝试实现一个时钟,但现在我无法更新标签。我尝试使用 print 语句来查看调用函数的天气情况,确实如此。但是标签没有更新?为什么会这样?

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()

.kv 文件

<watch@BoxLayout>:
    Label:
        text:root.time_str
    Button:
        on_press:root.incr()

<watch@BoxLayout> 更改为 <watch>。这为 python 代码中定义的小部件 class 创建了一个规则,而前者创建了一个全新的 class 定义,不需要 python 等效代码。

此外,您应该以大写字母开头来命名您的小部件。现在这对你来说并不重要,但是 kv 语言在某些情况下使用它来将它们与属性区分开来,所以这是一个很好的习惯(也是一个正常的 python 约定)。