Skip to content

Instantly share code, notes, and snippets.

@jmaicher
Created August 13, 2013 11:21
Show Gist options
  • Save jmaicher/6220192 to your computer and use it in GitHub Desktop.
Save jmaicher/6220192 to your computer and use it in GitHub Desktop.
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
Builder.load_string('''
<MyContainer>:
MyWidget:
<MyWidget>:
my_button: my_button
Button:
id: my_button
''')
class MyContainer(BoxLayout):
pass
class MyWidget(Widget):
my_button = ObjectProperty()
def __init__(self, **kwargs):
super(MyWidget, self).__init__(**kwargs)
print "MyWidget#__init__ has been executed"
def on_my_button(self, *args):
print "MyWidget#my_button has been set"
class MyApp(App):
def build(self):
# return MyWidget()
return MyContainer()
MyApp().run()
@jmaicher
Copy link
Author

class MyApp(App):
    def build(self):
        return MyWidget()
        # return MyContainer()
  1. MyWidget#my_button has been set
  2. MyWidget#init has been executed
class MyApp(App):
    def build(self):
        # return MyWidget()
        return MyContainer()
  1. MyWidget#init has been executed
  2. MyWidget#my_button has been set

Question: Is it expected behavior that the order of the output (see comment) changes and my_button is not directly available after calling super when the MyWidget instance is created via the markup language?

@jmaicher
Copy link
Author

The answer I got in #kivy:

qua-non: kivy tries to delay the building of the gui of the widgets till they are actually needed. So the children inside a widget won't be set untill the next frame after super is called. When manually initializing a widget, properties can be initialised before the call to init/super

=> Yes, the behavior is expected!

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