avoid parent.parent with properties or other ways

33 views
Skip to first unread message

Gouz

unread,
May 26, 2015, 1:20:14 AM5/26/15
to kivy-...@googlegroups.com

Thx to inclement's notice, I realized I was using some properties  the wrong way in my code. However, how could I make proper use of them and avoid parent.parent. ? Both python/kv side.
Advise I got (if I understood correctly) was to capture self into an object property.

Anyway, what is the proper way to get rid of parent.parent ?
Any example would be truly appriciated.

Also, I guess to navigate the widget tree one could make use of app in kv and get_running_app() in python which would get you right to the top. However I failed making that work too. (If possible, how could I use this way here too?)

All kind of corrections are trully welcome. Trying to learn some more about kivy power.

Thxxx

sk7...@gmail.com

unread,
May 26, 2015, 12:18:52 PM5/26/15
to kivy-...@googlegroups.com
You don't need to reference an object explicitly when binding a property to a KV id as far as I know.
The code below should link the object Button to the property first_level_button
#kv language example

<MainWindow>:
    first_lvl_button: first_lvl_button
    first_lvl: first_lvl
 
    FirstLevel:
        id: first_lvl
        orientation: "vertical"
    Button:
        id: first_lvl_button
        text: "Copy Label"
        on_release: root.copy_lbl()
 
# ... code removed


In order to prevent parent.parent syntax you could do this instead:

class CIMgui(App):
    def build(self):
        return MainWindow()
 
if __name__ == '__main__':
    maininstance = CIMgui()
    maininstance.run()

Your core App is now referable through the maininstance variable from anywhere in the python or KV code.

<SecondLevel>:
    second_lvl_label: second_lvl_label.__self__
 
    Label:
        id: second_lvl_label
        text: str(root.bool)
    Button:
        # OLD CODE
        #text: str(root.parent.parent.counter)

        text: str(app.root.counter)
        on_release: root.print_count()

Gouz

unread,
May 26, 2015, 3:20:40 PM5/26/15
to kivy-...@googlegroups.com
I get AttributeError: 'NoneType' object has no attribute 'counter'

ZenCODE

unread,
May 26, 2015, 5:02:54 PM5/26/15
to kivy-...@googlegroups.com
sk7 is on the right track here. Try changing that line to:

    text: str(app.root.counter) if app.root else ""
Reply all
Reply to author
Forward
0 new messages