Kivy Dynamically Adding Buttons

Viewed 35

I am a beginner to Kivy and I want add some simple buttons to my window. The problem is, that I need these buttons to be added dynamically. At the end, I want my programm to read latin sentences from a .txt file, and display them as buttons. I dont know how many sentences there will be, so this process needs to be made in the .py file. I got this very simple code that I understand. If you could base your answer on it, I would very much aprreciate it. And I also tried adding a background, there I was sucsessfull at least. :).

new.py:

import kivy.uix.button as kb
from kivy.app import App
from kivy.uix.widget import Widget


class Button_Widget(Widget):

    def __init__(self, **kwargs):
        super(Button_Widget, self).__init__(**kwargs)
        btn1 = kb.Button(text='Hello World 1')
        btn1.bind(on_press=self.callback)
        self.add_widget(btn1)

    def callback(self, instance):
        print('The button %s state is <%s>' % (instance, instance.state))


class ButtonApp(App):

    def build(self):
        return Button_Widget()


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

button.kv:

#:kivy 1.10.1

<Button_Widget>:
    canvas:
        Rectangle:
            pos: self.pos
            size: self.size
            source: 'background.png'

The code for the background:

canvas:
        Rectangle:
            pos: self.pos
            size: self.size
            source: 'background.png'
0 Answers
Related