简体   繁体   English

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

[英]AttributeError: 'NoneType' object has no attribute 'text' in python

I am using python-2.7 and kivy .When i run test.py then it gives error AttributeError: 'NoneType' object has no attribute 'text' in python ? 我正在使用python-2.7kivy 。当我运行test.py它给出错误AttributeError: 'NoneType' object has no attribute 'text' in python
Someone tell me what is mistake? 有人告诉我什么是错误的?

test.py test.py

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.properties import ObjectProperty


Window.size = (500, 230)


class GroupScreen(Screen):
    groupName = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(GroupScreen, self).__init__(**kwargs)
        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()

test.kv test.kv

GroupScreen:
    groupName:groupName

    GridLayout:
        cols: 2
        padding : 30,30
        spacing: 10, 10
        row_default_height: '40dp'

        Label:
            text: 'Test'

        SingleLineTextInput:
            id: groupName

        GreenButton:
            text: 'Ok'

        GreenButton:
            text: 'Cancel'

        Label:

        Label:



<SingleLineTextInput@TextInput>:
    multiline: False


<GreenButton@Button>:
    background_color: 1, 1, 1, 1
    size_hint_y: None
    height: self.parent.height * 0.150

If you are going to create the object in the .kv: 如果要在.kv中创建对象:

GroupScreen:
    groupName:groupName
    ...

Then it is not necessary to declare it in the .py. 这样就不必在.py中声明它。

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 . 另一方面,将子项添加到窗口小部件不是立即进行的,因此在这种情况下始终建议使用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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 (Python) AttributeError: &#39;NoneType&#39; 对象没有属性 &#39;text&#39; - (Python) AttributeError: 'NoneType' object has no attribute 'text' AttributeError:&#39;NoneType&#39;对象没有属性&#39;text&#39;-python - AttributeError: 'NoneType' object has no attribute 'text' - python Python:AttributeError:&#39;NoneType&#39;对象没有属性&#39;text&#39; - Python: AttributeError: 'NoneType' object has no attribute 'text' AttributeError: &#39;NoneType&#39; 对象没有属性 &#39;text&#39;, (xml, python) - AttributeError: 'NoneType' object has no attribute 'text', (xml, python) python错误AttributeError:&#39;NoneType&#39;对象没有属性&#39;text&#39; - python error AttributeError: 'NoneType' object has no attribute 'text' python - 'AttributeError: 'NoneType' object 在 web 抓取时没有属性 'text' - python - 'AttributeError: 'NoneType' object has no attribute 'text' when web scraping Python 错误消息 - AttributeError: 'NoneType' object 没有属性 'text' - Python Error Msg - AttributeError: 'NoneType' object has no attribute 'text' AttributeError:&#39;NoneType&#39;对象没有属性&#39;text&#39;-Python,BeautifulSoup错误 - AttributeError: 'NoneType' object has no attribute 'text' - Python , BeautifulSoup Error AttributeError:&#39;NoneType&#39;对象没有属性&#39;text&#39;beautifulsoup python - AttributeError: 'NoneType' object has no attribute 'text' beautifulsoup python Python-AttributeError:“ NoneType”对象没有属性“ get_text” - Python - AttributeError: 'NoneType' object has no attribute 'get_text'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM