Setup/Creating A Simple Application
Subscribe to Tech with Tim
What is Kivy?
Kivy is a python module that allows for the creation of cross compatible applications using python. It makes it very easy to reuse the same code on IOS, Andorid, Mac, Windows, Linux and virtually all other well known operating systems. Creating apps with kivy is great as your code works on every kind of device. Kivy is similar to TKinter in the way you develop apps. It allows you to create a GUI using widgets and layouts.
Installing Kivy
For detailed instructions on all operating systems please visit the kivy website.
Before we can start using kivy we must download and install it. The easiest way to do this is to use pip. To test if you have pip in your system path open up cmd and type pip.
If you do not receive any errors you can continue with the instructions.
If this did NOT work then you need to add pip to your system path. There are multiple ways to do this but the easiest is to modify your python installation and add pip. You can watch the video starting at 2:00 to see how to do this.
If pip is working then you need to type 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
If the execution of all of those commands is successful you are ready to move on.
Creating our First App
Once we have Kivy installed and setup we can get to creating our first app.
The first thing we need to do is import the necessary modules.
import kivy from kivy.app import App from kivy.uix.label import Label
The best way to create apps using Kivy is to do so using OOP. Therefore, we need to create a class to represent our window. This class will inherit from the App class that we imported above. This means it will take all functionally from App. In doing so all we have to do to make our class functional is implement a method build() which will tell Kivy what to place on the screen.
class MyApp(App): def build(self): return Label(text="tech with tim")
We simply return a label that says "tech with tim".
Running our App
Believe it or not we have just created our first app. To run it we need to add the following code to the end of our program.
if __name__ == "__main__": MyApp().run()
Now if we run the program we should see the following.
Full Code
import kivy from kivy.app import App from kivy.uix.label import Label class MyApp(App): def build(self): return Label(text="tech with tim") if __name__ == "__main__": MyApp().run()