'Store Kivy objects/ids in list

I need to store kivy object id-s (and some other info) in a list, but it does`nt seeme to work with kivy. there are checkboxes for every button, witch should enable/disable the buttons. the enabling/disabling should happen after a button is pressed (not immediately, like id could be done in kv file) I could write a punch of if,elif statements and it would work like that, but I will have a lot of buttons and different button lists so I would like to go through the checkboxses with for loop.

    #Here`s my python code:

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.properties import ObjectProperty

class MainLayout(GridLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        chk1 = ObjectProperty(None)
        btn1 = ObjectProperty(None)
        chk2 = ObjectProperty(None)
        btn2 = ObjectProperty(None)
        chk3 = ObjectProperty(None)
        btn3 = ObjectProperty(None)
        chk4 = ObjectProperty(None)
        btn4 = ObjectProperty(None)

        #(checkbox id, button id, value)
        self.object_list = [(chk1, btn1, 1),
                            (chk2, btn2, 2),
                            (chk3, btn3, 3),
                            (chk4, btn4, 4)]


    def myfunc(self):
        for i in self.object_list:
            if i[0].active == True:
                i[1].disabled = False

            elif i[0].active == False:
                i[1].disabled = True



class CheckboxTestApp(App):
    pass

if __name__=='__main__':
    CheckboxTestApp().run()



#and here`s the .kv
MainLayout:

<MainLayout>:

    chk1 : chk1
    btn1 : btn1
    chk2 : chk2
    btn2 : btn2
    chk3 : chk3
    btn3 : btn3
    chk4 : chk4
    btn4 : btn4


    cols: 4
    CheckBox:
        id: chk1
        active: True
    Button:
        id: btn1
        text: '1'
        on_press: root.myfunc()

    CheckBox:
        id: chk2
        active: True
    Button:
        id: btn2
        text: '2'
        on_press: root.myfunc()

    CheckBox:
        id: chk3
        active: True
    Button:
        id: btn3
        text: '3'
        on_press: root.myfunc()

    CheckBox:
        id: chk4
        active: True
    Button:
        id: btn4
        text: '4'
        on_press: root.myfunc()


Solution 1:[1]

I found a solution to my problem using kivy built in dictionary ids. I only updated the python code so here it is.

#python code
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.properties import ObjectProperty

class MainLayout(GridLayout):
    objects = [('chk1', 'btn1', 1),
               ('chk2', 'btn2', 2),
               ('chk3', 'btn3', 3),
               ('chk4', 'btn4', 4)]

    def myfunc(self):
        for i in self.objects:
            if self.ids[i[0]].active == True:
                self.ids[i[1]].disabled = False

            elif self.ids[i[0]].active == False:
                self.ids[i[1]].disabled = True





class CheckboxTestApp(App):
    pass

if __name__=='__main__':
    CheckboxTestApp().run()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MartinTammable