Getting Started
At the moment of writing, Kivy is at version 1.10.0 and it has a rich number of widgets.The most important widgets in Kivy are found in the kivy.uix
module.This module is what you will work with most of the time. It has widgets such as Button,Label,Switch, and other widgets are containers
meaning they can contain others. Such container widgets include FloatLayout,BoxLayout,GridLayout,RelativeLayout and more. Let us see the traditional hello world way of introducing a new concept.
from kivy.app import App
from kivy.uix.button import Button
class MainApp(App):
def build(self):
return Button(text='Hello World')
if __name__ == "__main__":
MainApp().run()
On running the code snippet above, the following is what you will see
Explanation of code
All the code thus is return a button with the text Hello World.
In the first line, we import the app.App class .Now the app class serves the purpose of returning what is generally known as the ==root widget
== in the app.App.build method. Every kivy application requires the App class since the root widget will be returned only in the App class. The so called root widget
is the whole UI that you want to render.In this our case, we used only a button as our root widget but in a bit complex apps, your root widgted will be mostly layouts like BoxLayout.
The second import is that of the button.As earlier mentioned, we will mostly be using stuff from kivy.uix module.In the next section, I will introduce the concept of layouts in pure Python code , before we get to know kivy's own magic kv lang.
Note
Every kivy application must have a class the inherits from kivy.app.App and the class must implement the build method before anything can be rendered to the screen.