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

前端 未结 3 1821
终归单人心
终归单人心 2020-12-07 03:03

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: \'vert         


        
3条回答
  •  一个人的身影
    2020-12-07 03:48

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

提交回复
热议问题