How can I make a lot of buttons at dynamic in kv language?

Viewed 2913

I want to make a lot of Buttons at dynamic in kv language. But now I cannot...... I will show now source under this.


BoxLayout:
    orientation: 'vertical'
    pos: root.pos
    size: root.size

    GridLayout:
        rows: 2
        spacing: 5
        padding: 5

        Button:
            text: "X0"
            on_press: root.X(0)
        Button:
            text: "X1"
            on_press: root.X(1)

I want to make like under code

BoxLayout:
    orientation: 'vertical'
    pos: root.pos
    size: root.size

    GridLayout:
        rows: 2
        spacing:5
        padding:5

        for i
            Button:
                text: "X#{i}"
                on_press: root.X(i)

How can I do?

3 Answers

This is false, you can easily integrate a loop in your KV file , like this :

    GridLayout:
    pos: root.pos
    size: root.size
    cols:30
    rows:3
    on_parent:
        for i in range(10): strId = "B{0}".format(i+1); i=Button(text=strId, id=strId, on_press=lambda BingoMain:root.my_Function(strId)); self.add_widget(i);
Related