- We first import
App
fromkivy.app
. This is the core class that controls your entire app — like a movie director! - Next, we create our own class
MyKivy
, which inherits fromApp
. - Inside that, we define the
build()
method — this is kind of like an initializer oronStart()
method in other frameworks. It runs when the app starts. - Lastly, the
if __name__ == "__main__"
block tells Python to run the app when this file is executed.
Let’s add a simple label so something shows up on the screen.
We’ll import Label
from kivy.uix.label
, and return it in our build()
function:
from kivy.uix.label import Label
class MyKivy(App):
def build(self):
return Label(text="Hello from Kivy!")
Now let’s talk about a cleaner and more professional way to write your UI using KV Language — Kivy’s own markup-style language.
It’s similar to HTML in concept — it separates your design (UI) from your logic (Python code).
Here’s how to do it using a KV
string inside your Python code:
from kivy.app import App
from kivy.lang import Builder
KV = '''
Label:
text: "Hello from KV!"
'''
class MyKivy(App):
def build(self):
return Builder.load_string(KV)
We’ve imported Builder
from kivy.lang
, created a simple KV
string with a label, and returned it using Builder.load_string()
.
Run the app, and you’ll see the same result — but the code is more organized and easier to read.
.kv
FileNow let’s take it one step further — using a completely separate .kv
file, just like using a separate HTML file in web development.
-
Create a new file named
mydesign.kv
-
Inside that file, just write:
Here’s the magic: Kivy will automatically load the .kv
file if its name matches the class name in lowercase (MyDesign
→ mydesign.kv
).
Run it — and voilà! Same output, but with much cleaner structure.
If you enjoyed this video, please like, subscribe, and hit the bell icon so you don’t miss the next lesson.
And if anything was unclear, drop a comment — I’m always here to help.
Thanks for watching — I’ll see you in the next tutorial.
Until then, peace and happy coding!
Social Media Accounts: