Python Kivy Button, Property and BoxLayout Example

In this post, you are able to find an example of Kivy Framework consists of a button and two text label in a boxlayout oriented vertically. There are two files and both are required to run this program. This program is suitable for both mobile and desktop versions of Kivy and has ability to resume at gaining focus when screen is locked or for another reason the program is put in background.

The first file is the Python file which contains controller part of the program:


from import App
from kivy.uix.boxlayout import BoxLayout

class RootWidget(BoxLayout):

class BasicButtonApp(App):
    def build(self):
        return RootWidget()
    def on_pause(self):
        return True
    def on_resume(self):
        return True

if __name__ == "__main__":

The second file is the Kivy file containing view part and making property binding(name this file as basicbutton.kv):

    orientation: "vertical"
    button_message: button_message
        text: "First Message"
        font_size: "50sp"
        id: button_message
        text: "Button has not pressed yet."
        font_size: "20sp"
        text: "Change Middle Message"
        on_press: button_message.text = "Button Pressed."; button_message.font_size = "50sp"

If you have any questions or suggestions about them, please do not hesitate to state them either in comments or via any of communication channels.

Note that I am using Crayon Syntax Highlighter plug-in for WordPress and unfortunately it does not support Kivy language highlighting at the time I am writing this post. So there is no true highlighting for .kv part.

comments powered by Disqus