I'm trying to create buttons with a for, but I want every button to have a different name, so I can later differenciate them and use them easily. As you can see on the screenshot, python doesn't accept the names for the button. There's probably an easy way to do this but I just don't know it and I couldn't find it by myself on forums or anything else. (Also I know I can use a GridLayout but I need BoxLayouts to every button have coordinates.) Thx for your help

Capture_d’écran_2021-12-01_155306.jpg

I would recommend copying and pasting the code in the forum using the Code Block (</>) button, rather than posting a screen shot. I get that in this case you wanted to highlight what the editor was telling you, but even so, it is hard for us to work with the code if we only have an image of it.

I don't think it is the name button that is being flagged as invalid, but rather that you are attempting to access an element in a list that doesn't exist (either the list element, nor the list itself).

What you want to do is assign an empty list to button, then use the append method to add the elements to the list.

I'd also mention that, rather than creating list_1 and list_2 and populating them manually without actually using them, you could instead use the range() function. Note that list indices are zero-based, not one-based.

Putting this all together, it would look something like this:

class Buttons(App):
    def build(self):
        button = list()
        for i in range(6):
            elem = BoxLayout(orientation='horizontal'))
            for j in range(10):
                button.append(Button(text='{}{}'.format(elem, j))
                elem.addWidget(button[j])

I can't say for certain that this is what you want, but it seems closer to the intended effect than the original version. I don't know enough about Kivy to say if more needed to be done to make is work as desired. As things stand, it seems like it would create six BoxLayout widgets, populating each with ten Button widgets, but then the BoxLayout widgets seem to fall off the edge of the world (since you aren't keeping a handle to them outside of the loop, never mind outside of the build() method) . My guess is that you'd have the BoxLayouts and their respective Buttons display, but then you wouldn't be able to do anything with them.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.