KivyMD Tutorial for Beginners: Create an Instagram Clone in Python!
Watch the video version of this post here: https://youtu.be/giSh7TDoCUMs
The best way to learn any framework or tool is to learn by building something. Kivy and kivymd are no different. You can use these tools to create android apps and IoS apps using Python programming language. In this tutorial, we will learn kivy and kivymd by making an Instagram Clone. So letâs begin. But before that make sure you have kivy and kivymd installed. If you use PyCharm IDE you will find the âPython Packagesâ section down at the bottom, you go there and search for whatever packages you want to install and install them directly.
Start by creating a new project in PyCharm and create a new .py
file named main.py
. This is entry point of your application which will branch to other screens and pages.
Part 1: The Basics of KivyMD
Letâs begin importing some stuff from kivymd library that weâll use:
Then we will create a class MainApp
, and in paranthesis ()
we write MDApp
. By doing this you are telling python that our class MainApp
extends the class MDApp
which we imported in the beginning.
When a class extends another class, it inherits all the functions of the parent class. Then we will define a function within our MainApp
class called build
.
We imported MDLabel
from kivymd library earlier. MDLabel
is a UI element which displays text. Our build function returns an MDLabel
with text âTestingâŚ1..2..3â and we set itâs horizontal alignment attribute halign
to center. Finally at the end of your code initialise our MainApp()
class:
Now run our main.py
program and you should see this:
This is the most basic Kivymd program that one can write. Hereâs the entire code:
Get ready guys, we are going to turn this hello-world program into Instagram. But first letâs make the size of the window of our application more phone-like:
Within the build(self)
function, add this as the first line of code:
But we have not imported Window
so add this in the beginning of your code:
Part 2: Designing Appbar, Modifying text, icon buttons and Box Layout.
Create a new file called home_page.kv
in the your project folder. We will the write the UI of our home-page screen in home_page.kv
using a very simple css-like syntax that comes with kivy. As you will see it makes thing a lot easier.
Add this code above the MainApp
class in main.py
:
We imported MDScreen
from kivymd and create a class named HomePage
which extends MDScreen
that we just imported. Since our class is empty, it will show an error. Just write some string with in the class to remove the error, like this:
Now letâs add stuff to home_page.kv
:
Writing <HomePage>:
tells kivy that the contents the lie ahead belong to the class HomePage
. Everything is a widget in kivy and kivymd. We used one widget in the beginning called MDLabel
for displaying text. Here we use another widget called MDBoxLayout
which lays out or organises elements on the screen. We set itâs padding to 5dp
. dp
is a unit of measurement and it stands for âdevice independent pixelsâ.
Within MDBoxLayout
we add a MDLabel
and set itâs attributes like we did earlier. But earlier we were using python and now we are using the KV language which is much simpler. You can checkout all the kivymd widgets and how to use them in kivymd documentation. Itâs very useful and we will refer to it all the time.
Letâs add some more properties to our MDLabel
and MDBoxLayout
:
I added vertical alignment and font size. Letâs change the font size as well. Go to this this link and download the Instafont.otf
. Create a new folder in your project folder called assets
and move Instafont.otf
in that directory. We will now change the font-name
attribute to âassets/Instafont.otfâ which is the address of the font file:
But wait! Our main.py
still dosenât know anything about home_page.kv
. Here is our code untill now:
Replace the return MDLabel...
argument of your build
function with this:
And we need to load home_page.kv
, so just above return HomePage()
add:
And import Builder
as well:
Now your code should look like this:
Run your code and the output should look something like this:
Our MDLabel
is pushed down! Donât worry we will fix that. Letâs add some icons in our toolbar/appbar, edit your home_page.kv:
We added two MDIconButton
widgets and set their icon-type. Now if you run your program you can see it looks better:
We want our toolbar on top of the screen, it will go on top after we add the add other components below it, so donât worry about it.
Before we do anything else letâs move our app bar under another MDBoxLayout
widget (we are going to add more Boxes which will contain stories tab and posts etc):
Below our app bar we will add a ScrollView which is widget in Kivy that allows us to create a scrollable list of items:
If you run the app, now you can see our app bar is placed at the top of the screen where it belongs:
You can learn more about ScrollView through the very well written documentation of kivy. I will leave the rest to you, hereâs the rest of the code for the instagram clone made with python using kivymd. Continue writing the program peice by peice, refer to the kivy and kivymd documentation and youâll figure it out. If you get stuck anywhere, you can dm me on twitter, i will try to make a video about it. Good Luck!