kivy python列表到按钮文本

时间:2016-02-24 18:00:26

标签: python kivy

尝试使用myList的内容填充三个按钮的文本[" One"," Two"," Three"] btn1 = myList [1]等

myList将从csv文件填充

from kivy.app import App

from kivy.lang import Builder

kv = """
    BoxLayout:

    Button:
        id: btn1
    Button:
        id: btn2
    Button:
        id: btn3
"""


class TestApp(App):

    def build(self):
        my_box = Builder.load_string(kv)
        my_ShowList = ['My Way', 'Wine Drinker', 'Boots']

        '''
        This is where I get lost
        want to use for loop to populate Button text with my_ShowList items
        '''


        return my_box


if __name__ == '__main__':

    TestApp().run()enter code here`

1 个答案:

答案 0 :(得分:0)

你可以完全用python代码完成:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button


class TestClass(App):
    def build(self):
        my_box = BoxLayout()
        my_show_list = ["My Way", "Wine Drinker", "Boots"]
        my_box.my_buttons = [] # if you want to keep an "easy" reference to your buttons to do something with them later
                               #kivy doesnt crashes because it creates the property automatically
        for message in my_show_list:
            button = Button(text=message)
            my_box.my_buttons.append(button)
            my_box.add_widget(button)
        return my_box


if __name__ == "__main__":
    TestClass().run()