【问题标题】:AttributeError: 'super' object has no attribute '__getattr__' in .kvAttributeError:“超级”对象在 .kv 中没有属性“__getattr__”
【发布时间】:2021-08-11 12:56:59
【问题描述】:

所以我正在使用 .kv 文件并尝试使用文本输入 然后我想清除文本输入 所以我写了这段代码

Python

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.uix.textinput import TextInput
Builder.load_file("my.kv")
class MyLayout(Widget):
    def __init__(self,**kwargs):
        super(MyLayout,self).__init__(**kwargs)
    def clear(self):
        self.ids.n.text = ""
        self.ids.fp.text = ""
        self.ids.d.text = ""


class AwesomeApp(App):
    def build(self):
        return MyLayout()
if __name__ == '__main__':
    AwesomeApp().run()

.kv

<Button>
    font_size:20
<TextInput>
    font_size:20
<Label>
    font_size:20
<MyLayout>
    BoxLayout:
        orientation:"vertical"
        size: root.width,root.height
        padding:10
        spacing:10
        Label:
            text:"Name"
        TextInput:
            id:"n"
            multiline:False
        Label:
            text:"favorite Pizza"
        TextInput:
            id:"fp"
            multiline:False
        Label:
            text:"Drinks"
        TextInput:
            id:"d"
            multiline:False
        Button:
            text:"Submit"
        Button:
            text:"clear fields"
            on_press:root.clear()

当我点击“清除字段”按钮时它会抛出这个错误

AttributeError: 'super' object has no attribute '__getattr__'

【问题讨论】:

    标签: kivy kivy-language python-3.8


    【解决方案1】:

    您的ids 不应该是字符串。例如,改变:

        TextInput:
            id:"n"
    

    到:

        TextInput:
            id: n
    

    另一个TextInputs也类似。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-20
      • 1970-01-01
      • 2021-07-12
      相关资源
      最近更新 更多