且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

AttributeError:' NoneType'对象没有属性' text'在python中

更新时间:2022-05-22 15:15:29

如果要在.kv中创建对象,则:

If you are going to create the object in the .kv:

GroupScreen:
    groupName:groupName
    ...

那么就没有必要在.py中声明它.

Then it is not necessary to declare it in the .py.

另一方面,将子项添加到窗口小部件不是立即进行的,因此在这些情况下始终建议使用 Clock .

On the other hand the addition of children to a widget is not instantaneous so it is always recommended in these cases to use Clock.

import kivy

kivy.require('1.9.0')  # replace with your current kivy version !
from kivy.uix.screenmanager import Screen
from kivy.app import App
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.clock import Clock


Window.size = (500, 230)


class GroupScreen(Screen):
    def __init__(self, **kwargs):
        super(GroupScreen, self).__init__(**kwargs)
        Clock.schedule_once(lambda dt: setattr(self.groupName, 'text', "Test"))

class Group(App):
    def build(self):
        self.root = Builder.load_file('test.kv')
        return self.root


if __name__ == '__main__':
    Group().run()