AttributeError:“NoneType”对象没有属性“ids”(自我.root返回'None')

2024-05-23 16:27:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从我的.py文件访问.kv文件中按其ID定义的按钮/标签,并更改其“text”值。 所以我在构建函数中写道:

#self.root.ids.button.text='newValue'

上面写着AttributeError: 'NoneType' object has no attribute 'ids' 我试过很多方法自我.root“从班上其他地方来的,但都不管用。在

我不知道为什么我没有自我.root在我班?在

这是我的kv文件:

<MyRun>:

    BoxLayout:
        id:bx1
        orientation: 'vertical'
        spacing: 10
        padding: [20]
        canvas:
            Color:
                rgba: .6, .6, .6, .3
            Rectangle:
                size: root.size
                source: 'pic\germanFlag.png'

        ActionBar:
            ActionView:
                id: av
                ActionPrevious:
                    with_previous: False
                    title: 'DeutschLerne' 
        Label:
            id:qtitle
            text: 'Percentage of Question Answered:{}%'.format(int(7))
            font_size: '30sp'
            size_hint_y: None
            height: '48dp'

        Label:
            id:qbody
            text: 'The answer is '+('{}'.format(str("Richtig!")) if 1>0 else '{}'.format(str("Falsch!")))
            color: [.3, 1, .5, 1] if 1>0 else [1, .2, .3, 1]
            font_size: '30sp'
            size_hint_y: None
            height: '48dp'

        Widget:
            canvas:
                Color:
                    rgba: .6 , .6 , .6 , .3
                Rectangle:
                    source: "pic\questionCanvas.png"
                    size: [self.width,self.height*3]
                    pos: [self.x,self.y-300]


        BoxLayout:
            id:bx2
            spacing: 10
            padding: [10, 200 , 10 , 0]

            Button:
                id:selA
                text: 'A'
                size_hint_y: None
                size_hint_x: .5
                on_release: app.answer(self.text)
                on_release: app.disableButton(self)
            Button:
                id:selB
                text: 'B'
                size_hint_y: None
                size_hint_x: .5 
                on_release: app.answer(self.text)
                on_release: app.disableButton(self)
            Button:
                id:selC
                text: 'C'
                size_hint_y: None
                size_hint_x: .5
                on_release: app.answer(self.text)
                on_release: app.disableButton(self)
            Button:
                id:selD
                text: 'D'
                size_hint_y: None
                size_hint_x: .5
                on_release: app.answer(self.text)
                on_release: app.disableButton(self)
                #on_release: app.go_screen(0)

        ProgressBar:
            id: pb
            size_hint_x: 1
            size_hint_y: None
            height: '48dp'
            value: (app.time * 20) % 100.

这是我的主修课,Desutsch.py公司在

^{pr2}$

我是新来的。对不起,这些丑陋的代码。我还在学习如何问一个好问题。在


Tags: 文件textanswerselfnoneidappsize
1条回答
网友
1楼 · 发布于 2024-05-23 16:27:33

根属性被设置为从构建方法返回的任何值,因此当您试图访问它时它不存在(或者更确切地说,它的默认值是None)。在

不过,您不需要它,因为您无论如何都有对小部件的引用。可以将生成方法的结尾更改为

root = MyRun()
root.ids.selA.text='newValue'
return root

相关问题 更多 >