Setup & Creating A Simple Kivy ApplicationLast Update : 19 Sep, 2022
What is Python Kivy?
Kivy is a Python module that allows the creation of cross-platform compatible graphical user interface (GUI) applications using Python. We can run Kivy applications on iOS, Android, Windows, OS X, and GNU/Linux.
Kivy allows you to create a GUI using widgets and layouts. Also, It is similar to Python Tkinter in the way you develop applications.
Installing Kivy on Your PC
Before start developing the Kivy apps, We must download and install Kivy on Our PC. Using the pip command is the easiest way to download and install Kivy on Our PC.
So, we can test the pip command running on your system by opening CMD and typing pip.
If any error is not received, can continue with the instructions.
If the pip command does not work on your PC, need to add pip to your system path.
So, There is more than one way to add pip to your system path. But, the easiest way is to modify your python installation and add pip to the system path.
If the pip command is working, You need to install Kivy by typing the following commands into your command prompt.
python -m pip install --upgrade pip wheel setuptools python -m pip install docutils pygments pypiwin32 kivy.deps.sdl2 kivy.deps.glew python -m pip install kivy.deps.gstreamer python -m pip install kivy.deps.angle python -m pip install pygame python -m pip install kivy
Creating Hello World Kivy App
If your successfully installed kivy library on your computer, Let's create our first Python Kivy app.
First, You need to create a Python file with the .py extension.
After that, You need to import the necessary kivy modules.
For example -:
import kivy from kivy.app import App from kivy.uix.label import Label
Object Oriented Programming (OOP) is the best way to create kivy app. Therefore, You also can create your first app using OOP.
Now, You need to create a class to represent our window which will inherit from the App class. And write the main block that prints "Hello World".
class FirstKivyApp(App): def build(self): return Label(text ="Hello World")
This will return a label with the text "Hello World".
To run this app You need to write the following code to at the end of the program.
if __name__ == "__main__": MyApp().run()
The complete program is as follows.
import kivy from kivy.app import App from kivy.uix.label import Label # Defining a class class FirstKivyApp(App): # Function that returns the root widget def build(self): # Hello World text Label return Label(text ="Hello World") if __name__ == "__main__": # This initializes and starts our app FirstKivyApp().run()
You can see the following window with a "Hello World" text Label by running this program.
This is a simple demonstration of creating and running a simple Kivy Hello World app. You can learn more about what happens to this code in the Kivy application development basics tutorial.