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

kivy button kv
kivy button size
kivy access widget by id
kivy ids
kivy add widget dynamically
kivy listview

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?

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

Kv language — Kivy 1.11.1 documentation, The KV language, sometimes called kvlang or the kivy language, allows you to To do the same thing in kv, you can set properties of the child widget directly in the rule: There are no quotes: good -> id: value , bad -> id: 'value' Therefore, after the remove button is clicked (which removes any direct Dynamic Classes� . Button: The Button is a Label with associated actions that are triggered when the button is pressed (or released after a click/touch). We can add functions behind the button and style the button. In this article, we are going to discuss how we can create the buttons using .kv file. We do a little bit of button styling also and also we define

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

Python, In this article, we are going to discuss how we can create the buttons using .kv file . We do a little bit of button styling also and also we define you how to bind a� To create a dynamic size for our buttons we will set the size_hint for each of our buttons to be 0.5, 0.5 . This will make each of our buttons take up 50% width and 50% height of the container they are in. When creating a dynamic position we can define up to 6 keys in a dictionary like so:

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()

Python, size_hint : This is for dynamic sizing of the button and provide hint of size. create Layout class 6) create App class 7) Set up .kv file : Create the buttons and set� Creating a Form With a .kv File. The file below is the recreation of the form we made in tutorial 3, this time using a kv file. You can see that we have multiple grid layouts like before and that we add widgets to those layouts. Notice that to make these widgets fill the entire screen we must change the size attribute.

Dynamic Resizable Placement, Kivy with Python tutorial Part 5 - Dynamic Resizable Placement SimpleKivy3( App): def build(self): return Widgets() if __name__ == "__main__": SimpleKivy3(). run() Button: pos: root.x, root.top - self.height text: "Kivy" Button: pos: 170,0 text: "Tutorials" Kivy .kv Language cont'd Screen Manager for Multiple Screens. Kivy language reminds me a lot of CSS, or stylesheets, for HTML. While you could write all of your styles in line with your HTML, it just makes more sense to separate it. There exists 2 quiz/question(s) for this tutorial. Sign up to +=1 for access to these, video downloads, and no ads. The next tutorial: Kivy .kv Language cont'd

Kivy with Python tutorial Part 5, It's helping me out a lot. Could you possibly show how to create buttons and the likes using Duration: 5:44 Posted: 22 Feb 2015 Button¶. The Button is a Label with associated actions that are triggered when the button is pressed (or released after a click/touch). To configure the button, the same properties (padding, font_size, etc) and sizing system are used as for the Label class:

Kivy Tutorial Python, Anything indented underneath this tag will appear and change our FloatLayout. To add two buttons to our layout we do the following. <FloatLayout>: Button: text: "� The KV Language is an attempt to overcome these shortcomings. The KV language, sometimes called kvlang or the kivy language, allows you to create your widget tree in a declarative way and to bind widget properties to each other or to callbacks in a natural manner. It allows for very fast prototypes and agile changes to your UI.

Comments
  • 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
  • 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.
  • Indeed! I was just pointing out that the question may not necessarily have been the right question.
  • 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.