0

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?
Someone tell me what is mistake?

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

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
3
  • No idea about kivy. Check why self.groupName is None in GroupScreen.__init__ method. Commented Mar 22, 2018 at 6:08
  • You don't mean to use "None" as parameter to ObjectProperty. See stackoverflow.com/questions/18580794/… Commented Mar 22, 2018 at 6:13
  • Can you paste your whole error too! Commented Mar 22, 2018 at 6:19

1 Answer 1

2

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

GroupScreen:
    groupName:groupName
    ...

Then it is not necessary to declare it in the .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.

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()
Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.