0

I can't comment yet, so forgive the related post. I'm having the same problem as this person: Empty list of ids in a Kivy GridLayout

Though I've found a workaround as the answerer suggests, by storing references to my gridded objects as they're created, I would like to know if there IS a way to programmatically assign ids to widgets that can later be accessed through the parent's "ids" dictionary.

For example, in the original question, the code adds widgets like this:

for i in range(81):
    row = i // 9
    col = i  % 9
    grid.add_widget(TextInput(id = str(row) + "-" + str(col)))

but the id property used here is apparently different than the id property if you assign it in a kv file.

So the expected output is that the ids dict would look something like:

{"1,1": *objectreference@blahblah*, "1,2": *objectreference@blahblah*, .....}

but the actual output is: {}

Is there a way to make that work as expected? Relatedly, is it worth it to find that way/ is it better practice to create your own reference dictionary instead?

1 Answer 1

1

Use built-in children list, which is automatically populated with every added widget:

class MyButton(Button):

    x = NumericProperty(-1)
    y = NumericProperty(-1)


class MyGrid(GridLayout):

    cols = NumericProperty(1)

    def add_buttons(self):
        for i in xrange(10):
            button = MyButton(x=i, y=i)
            self.add_widget(button)

    def print_children(self):
        for child in self.children:
            print 'button coords:', child.x, child.y
Sign up to request clarification or add additional context in comments.

2 Comments

So if I'm understanding correctly, there isn't a way to add to the ids list unless you do it from the kivy side, but there are various other ways you can store references to the individual objects. Is that correct? How would you reference each individual button using your code above?
@KSully2 The ids dict is populated by parsing kv file. Even if it was possible to somehow append, there is no need of doing it. You could reference these button in many ways, one of them is written in the answer. If you want to access them like with ids dict, then simply create a new dict and put references to them there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.