Python Kivy返回'AttributeError:'super'对象没有属性'__getattr __''

问题描述 投票:0回答:2

我正在尝试使用Kivy构建交互式的学校时间表,但是我一直遇到根据按钮名称更改Button文本的问题。我创建了一个网格布局,其中每个按钮都有一个唯一的名称,例如,星期一的第一个Button是one_mon,下一个是two_mon,依此类推。我创建了一个从Button继承的类,这是该类的Kivy和Python代码:

<Tile>:
    background_color: [.5, .9, 1, 1]
    halign: "center"
    size_hint: None, None
    size: 96, 96
    text: self.lesson
    on_press: self.on_press()
    on_release: self.on_release()

这是Tile的Python代码


class Tile(Button):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.lesson = ""
        self.sub_press = ""
        self.check()
        self.text = self.lesson

    def check(self):
        if self.ids.one_mon == "one_mon":
            self.text = "English"
            self.sub_press = "Room nr. 42 \n Mr. Hetman"

        ...        

        else:
            self.lesson = "None"
            self.sub_press = "None"

    def on_release(self):
        self.text = self.lesson
        self.background_color = [.5, .9, 1, 1]

    def on_press(self):
        self.text = self.sub_press
        self.background_color = [.01, .9, 1, 1]

这里是错误消息if self.ids.one_mon.name == "one_mon": File "kivy\properties.pyx", line 863, in kivy.properties.ObservableDict.__getattr__ AttributeError: 'super' object has no attribute '__getattr__'

这是将所有这些按钮组合在一起的父窗口小部件的代码:

<PlanChart>:
    cols: 11
    padding: 2
    Tile:
        id: one_mon
        name: "one_mon"
    Tile:
        id: two_mon
        name: "two_mon"
    Tile:
        id: three_mon
        name: "three_mon"

    ......

    Tile:
        id: ten_fri
        name: "ten_fri"

为什么会出现此错误?还有其他方法可以检查按钮的ID吗?也许我应该完全放弃这个项目,并开始使用不同的策略?欢迎任何帮助

python python-3.x user-interface kivy kivy-language
2个回答
0
投票

问题可能来自您从Button类继承的方式以及您如何将属性绑定到按钮上

您应该继承按钮类,并覆盖on_release方法

方法重写后,单词self.text将被作为当前按钮的内文显示为准

from kivy.properties import ObjectProperty

class Tile(Button):

    def __init__(self, **kwargs):
        super(Tile, self).__init__(**kwargs)
        self.lesson = ""

    def on_release(self):
        self.text = self.lesson

0
投票

问题可能来自您从Button类继承的方式以及您如何将属性绑定到按钮上

您应该继承按钮类,并覆盖on_release方法

方法重写后,单词self.text将被作为当前按钮的内文显示为准

class Tile(Button):

    def __init__(self, **kwargs):
        super(Tile, self).__init__(**kwargs)
        self.lesson = ""

    def on_release(self):
        self.lesson = "lesson text has changed"
        self.text = self.lesson
© www.soinside.com 2019 - 2024. All rights reserved.