Skip to content

Instantly share code, notes, and snippets.

@nlowe
Last active September 5, 2015 20:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nlowe/363703dc2ac0538ddc2b to your computer and use it in GitHub Desktop.
Save nlowe/363703dc2ac0538ddc2b to your computer and use it in GitHub Desktop.
from kivy.app import App
from kivy.lang import Builder
from kivy.properties import ObjectProperty
from kivy.uix.label import Label
from kivy.uix.widget import Widget
UI = """
#:kivy 1.9.0
<ChildWidget>:
grid: grid_layout
GridLayout:
id: grid_layout
cols: 3
<MainWindow>:
child: child
ChildWidget:
id: child
"""
class ChildWidget(Widget):
grid = ObjectProperty(None)
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.grid.add_widget(Label(text="Test"))
# ^^^
#Traceback (most recent call last):
# File "/home/nathan/projects/KivyTest/src/ssce.py", line 42, in <module>
# SSCE().run()
# File "/usr/lib/python3.4/site-packages/kivy/app.py", line 798, in run
# root = self.build()
# File "/home/nathan/projects/KivyTest/src/ssce.py", line 39, in build
# return MainWindow()
# File "/usr/lib/python3.4/site-packages/kivy/uix/widget.py", line 271, in __init__
# Builder.apply(self)
# File "/usr/lib/python3.4/site-packages/kivy/lang.py", line 1872, in apply
# self._apply_rule(widget, rule, rule)
# File "/usr/lib/python3.4/site-packages/kivy/lang.py", line 1983, in _apply_rule
# child = cls(__no_builder=True)
# File "/home/nathan/projects/KivyTest/src/ssce.py", line 29, in __init__
# self.grid.add_child(Label(text="Test"))
# AttributeError: 'NoneType' object has no attribute 'add_child'
class MainWindow(Widget):
child = ObjectProperty(None)
class SSCE(App):
def build(self):
Builder.load_string(UI)
return MainWindow()
if __name__ == '__main__':
SSCE().run()
@nlowe
Copy link
Author

nlowe commented Sep 5, 2015

From #kivy on irc.freenode.net:

<inclement> I think self.grid just isn't set until after the __init__
<inclement> Because when the kv is loaded, it has to instantiate ChildWidget before it adds the child
<me> So I would have to do something like Clock.schedule_once(self.__my_custom_init__)?
<inclement> I think so

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment