Clement Puech
Clement Puech

Reputation: 115

Kivy nested IDs

I'm trying to create a customer management software, so I need to create a GUI. I chose Kivy because it's Open Source and LGPL.

This software is meant to have multiple panels, so I need to have ID's to access widgets in each panel. I created Kivy rules in kv language, but when I nest a class is another one, I can't access the ID's. Below an example code:

LayoutTestApp.py :

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.uix.boxlayout import BoxLayout


class SkipperList(GridLayout):
    pass


class TestPanel(BoxLayout):
    def __init__(self, **kwargs):
        super(TestPanel, self).__init__(**kwargs)
        print "TestPanel ids:", self.ids


class MasterPanel(TabbedPanel):
    pass


class NjordApp(App):
    def __init__(self, **kwargs):
        super(NjordApp, self).__init__(**kwargs)

    def build(self):
        root = MasterPanel()
        return root

if __name__ == '__main__':
    application = NjordApp()
    application.run()

njord.kv

#:kivy 1.9.0

<MasterPanel>
    pos_hint: {'center_x': .5, 'center_y': .5}
    do_default_tab: False

    TabbedPanelItem:
        text: 'Skippers'
        BoxLayout:
            padding: 10
            spacing: 10
            TestPanel:

<TestPanel>:
    id: SkipperPanelId
    BoxLayout:
        padding: 10
        spacing: 10
        BoxLayout:
            orientation: 'vertical'

            Label:
                text: 'List des mecs'
                size_hint: 1, 0.09
            Button:
                id: button_up
                size_hint: 1, 0.08
                text:'/\\'
            Button:
                id: button_down
                size_hint: 1, 0.08
                text:'\/'

When I launch the software, the print only return {}. Can someone tell me how to access button_up ID for example ? Thanks advance.

Upvotes: 1

Views: 1233

Answers (1)

bj0
bj0

Reputation: 8213

The reason you don't see the ids is because you are printing in the constructor of TestPanel. It hasn't finished being created yet, let alone have anything added to it. If you print the ids after the GUI has been created (ie, from a button press) then you will see the ids:

class TestPanel(BoxLayout):
    def __init__(self, **kwargs):
        super(TestPanel, self).__init__(**kwargs)
        print "TestPanel ids:", self.ids

    def test(self, *x):
        print self.ids


...

        Button:
            id: button_up
            size_hint: 1, 0.08
            text:'/\\'
            on_press: root.test()

output:

{'button_down': <WeakProxy to <kivy.uix.button.Button object at 0x7f63159052c0>>, 'button_up': <WeakProxy to <kivy.uix.button.Button object at 0x7f63158f3738>>}

Upvotes: 1

Related Questions