Kivy无法更改标签文字

时间:2015-11-16 16:01:47

标签: android python ios kivy

我正在尝试使用Kivy创建一个简单的脚本:

 def __init__(self):
    super(ProvaLayout, self).__init__()
    self.cols = 2
    self.Labell = Label(text="-------------")
    self.add_widget(self.Labell)
    self.btn = Button(text="caio")
    self.add_widget(self.btn)
    def callback(self, pos, instance):
        self.Labell=Label(text="caio")
    self.btn.bind(on_pressed = callback)

但是,上述内容不会更改labell的文字。

1 个答案:

答案 0 :(得分:0)

更正后的版本。评论中描述了错误。

def __init__(self):
    super(ProvaLayout, self).__init__()
    self.cols = 2
    self.Labell = Label(text="-------------")
    self.add_widget(self.Labell)
    self.btn = Button(text="caio")
    self.add_widget(self.btn)         
    def callback(instance): # single argument without self (it's a local function inside a method)
        self.Labell.text="caio"  # changing text property of existing self.Label instead of creating a new object       
    self.btn.bind(on_press = callback) # you should use on_press, not on_pressed