如何通过Clock.schedule_interval更改标签文本

时间:2016-02-04 20:17:13

标签: python class kivy

在下面的代码中更改测试标签(使用my_callback函数)的正确方法是什么:

class Hello(FloatLayout):
    def __init__(self,**kwargs):
        super(Hello,self).__init__(**kwargs)
        self.Test = Label(text = "Aaa", pos=(0,0), font_size=70)
        self.add_widget(self.Test)

    def my_callback(self):
        print('My callback is called')
        self.Test.text = "Bbb"

    Clock.schedule_interval(my_callback, 3)

class app1(App):
    def build(self):
        return Hello()

if __name__=="__main__":
    app1().run()

现在有错误:

  

AttributeError:'float'对象没有属性'Test'

自我呼叫可能有问题吗?

1 个答案:

答案 0 :(得分:1)

class Hello(FloatLayout):
    def __init__(self,**kwargs):
        super(Hello,self).__init__(**kwargs)
        self.Test = Label(text = "Aaa", pos=(0,0), font_size=70)
        self.add_widget(self.Test)
        Clock.schedule_interval(self.my_callback, 3)
    def my_callback(self, dt=0):
        print('My callback is called')
        self.Test.text = "Bbb"

你可能想先看看一些基本的python教程,然后再跳到kivy ......