4

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?

Peter Badida
  • 11,310
  • 10
  • 44
  • 90
snow
  • 41
  • 1
  • 5

3 Answers3

1

Such loops aren't possible in kv language, other than doing some dirty hacks.

To create a set of buttons dynamically, either use ListView or add them in a loop inside a py file.

Example:

from functools import partial

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

    def add_buttons(self):
        for i in xrange(5):
            button = Button(
                text='X' + str(i),
                on_press=partial(self.X, number=i)
            )
            self.add_widget(button)

    def X(self, caller, number):
        print  caller, number
jligeza
  • 4,544
  • 6
  • 23
  • 31
  • Actually, you'd have to `self.add_widget(Button(text='X' + str(i), on_press=lambda sender, i=i:self.parent.X(i)))`, which highlights why one may want to consider alternative options – zeeMonkeez Mar 08 '16 at 14:19
0

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);
-1

I don't think this can be done in an kv file. However, if you can write the kv string in your python file, you can do something like this:

from kivy.app import App
from kivy.lang import Builder

kv_string = """
BoxLayout:
    orientation: 'vertical'
    pos: root.pos
    size: root.size

    GridLayout:
        rows: 2
        spacing: 5
        padding: 5
""" + ''.join(["""
        Button:
            text: "X{0}"
            on_press: root.X({0})
""".format(i) for i in range(6)])

class MyApp(App):
    def build(self):
        w = Builder.load_string(kv_string)
        return w

if __name__ == '__main__':
    MyApp().run()
zeeMonkeez
  • 5,057
  • 3
  • 33
  • 56
  • Another way to do it would be to just create the layout in KV and then add the buttons in a loop in the python code. Not everything has to be done in kv. – elParaguayo Mar 08 '16 at 09:58
  • Indeed! I was just pointing out that the question may not necessarily have been the right question. – elParaguayo Mar 08 '16 at 11:33
  • 1
    I do agree ... Trying to think of cases when I would actually use this construct ... And I cannot really come up with a case. But it can be done. – zeeMonkeez Mar 08 '16 at 12:17