Getting Started with Kivy — Your Friendly, Humanized Guide
Welcome! If you're here, you're probably curious about building beautiful, cross-platform apps with Python. Kivy is your golden ticket. This guide walks you through 15 essential steps — each with real code, friendly explanations, and zero jargon overload. Let's turn you from “What's Kivy?” to “Look what I built!”
1. Installing Kivy Without Tears
First things first — let's get Kivy on your machine. We'll use pip, Python's package manager. Don't worry if you've never done this before; we've got you.
pip install kivy[base] kivy_examples
💡 Pro Tip: If you're on Windows and run into issues, try installing in a virtual environment or use
pip install kivy --pre --upgrade
for the latest version.
2. Your First Kivy App — “Hello, Universe!”
Let's write the simplest Kivy app possible. This one just shows a label. Think of it as your “Hello World” moment — but cooler.
from kivy.app import App
from kivy.uix.label import Label
class HelloUniverseApp(App):
def build(self):
return Label(text='Hello, Universe!')
HelloUniverseApp().run()
Save this as main.py
and run it. Boom — window with text! You're officially a Kivy developer now.
3. Understanding the App Class and build()
The App
class is your app's brain. The build()
method returns the root widget — the
starting point of your UI. Everything flows from here.
from kivy.app import App
from kivy.uix.button import Button
class MyApp(App):
def build(self):
# This button becomes the entire UI
return Button(text="Click Me (I dare you)")
MyApp().run()
Run this and you'll see a big button. Click it. Nothing happens yet — but soon, oh soon.
4. Adding Layouts — Because Buttons Need Friends
One widget is cute. Ten widgets? That's a party. Use layouts to organize them. BoxLayout stacks widgets vertically or horizontally.
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
class PartyApp(App):
def build(self):
layout = BoxLayout(orientation='vertical')
layout.add_widget(Label(text="Welcome to the Widget Party!"))
layout.add_widget(Button(text="Dance Button"))
layout.add_widget(Button(text="Nap Button"))
return layout
PartyApp().run()
Now you've got a vertical stack of widgets. Try changing orientation='horizontal'
to see them side
by side.
5. Making Buttons Actually Do Something
Buttons that don't do anything are sad. Let's give them purpose. We'll bind a function to the button's
on_press
event.
from kivy.app import App
from kivy.uix.button import Button
class ActionButtonApp(App):
def build(self):
btn = Button(text='Click for Magic')
btn.bind(on_press=self.do_magic)
return btn
def do_magic(self, instance):
print("✨ Magic happened! Button text changed.")
instance.text = "You did magic!"
ActionButtonApp().run()
Click the button. Watch the text change. Feel the power.
6. Using KV Language — Kivy's Secret Sauce
KV language is Kivy's way of separating design from logic. It's like CSS for Python apps. Create a file called
myapp.kv
in the same folder.
#:kivy 2.0
:
text: "I'm styled with KV!"
background_color: 0.2, 0.6, 1, 1
font_size: 20
Then in your Python file:
from kivy.app import App
from kivy.uix.button import Button
class MagicButton(Button):
pass
class KVApp(App):
def build(self):
return MagicButton()
KVApp().run()
Kivy auto-loads myapp.kv
if your app class is named KVApp
. Magic!
7. Handling Multiple Screens with ScreenManager
Real apps have multiple screens. Kivy's ScreenManager
lets you slide between them like a pro.
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
class HomeScreen(Screen):
def __init__(self, **kwargs):
super().__init__(**kwargs)
layout = BoxLayout()
btn = Button(text="Go to Settings")
btn.bind(on_press=self.go_to_settings)
layout.add_widget(btn)
self.add_widget(layout)
def go_to_settings(self, instance):
self.manager.current = 'settings'
class SettingsScreen(Screen):
def __init__(self, **kwargs):
super().__init__(**kwargs)
layout = BoxLayout()
btn = Button(text="Back Home")
btn.bind(on_press=self.go_home)
layout.add_widget(btn)
self.add_widget(layout)
def go_home(self, instance):
self.manager.current = 'home'
class MultiScreenApp(App):
def build(self):
sm = ScreenManager()
sm.add_widget(HomeScreen(name='home'))
sm.add_widget(SettingsScreen(name='settings'))
return sm
MultiScreenApp().run()
Now you can navigate between two full screens. Add animations by setting
sm.transition.direction = 'left'
.
8. Styling Widgets with Backgrounds and Colors
Let's make things pretty. Kivy widgets can be styled with colors, images, and even custom graphics.
from kivy.app import App
from kivy.uix.button import Button
from kivy.graphics import Color, Rectangle
class StyledButton(Button):
def __init__(self, **kwargs):
super().__init__(**kwargs)
with self.canvas.before:
Color(0.9, 0.3, 0.3, 1) # Reddish background
self.rect = Rectangle(size=self.size, pos=self.pos)
self.bind(size=self._update_rect, pos=self._update_rect)
def _update_rect(self, instance, value):
self.rect.pos = instance.pos
self.rect.size = instance.size
class PrettyApp(App):
def build(self):
return StyledButton(text="I'm a Fancy Button", color=(1,1,1,1))
PrettyApp().run()
This button draws a custom background. Resize the window — the background stays perfect.
9. Responding to Touch and Gestures
Kivy is built for touch. Override on_touch_down
, on_touch_move
, and
on_touch_up
to handle gestures.
from kivy.app import App
from kivy.uix.label import Label
class TouchLabel(Label):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.text = "Touch me anywhere!"
self.font_size = 24
def on_touch_down(self, touch):
self.text = f"Touched at: {int(touch.x)}, {int(touch.y)}"
def on_touch_move(self, touch):
self.text = f"Dragging at: {int(touch.x)}, {int(touch.y)}"
def on_touch_up(self, touch):
self.text = f"Released at: {int(touch.x)}, {int(touch.y)}"
class TouchApp(App):
def build(self):
return TouchLabel()
TouchApp().run()
Touch, drag, release — watch the coordinates update in real time. Works with mouse too!
10. Loading Images and Icons
Visuals matter. Let's load an image. Put any image (e.g., logo.png
) in your project folder.
from kivy.app import App
from kivy.uix.image import Image
from kivy.uix.boxlayout import BoxLayout
class ImageApp(App):
def build(self):
layout = BoxLayout()
img = Image(source='logo.png', size_hint=(1, 0.8))
layout.add_widget(img)
return layout
ImageApp().run()
If the image doesn't show up, double-check the filename and path. Kivy looks in the same directory as your script.
11. Playing Sounds and Music
Games and apps need sound. Kivy's SoundLoader
makes it easy. Place an audio file (e.g.,
beep.wav
) in your folder.
from kivy.app import App
from kivy.uix.button import Button
from kivy.core.audio import SoundLoader
class SoundApp(App):
def build(self):
self.sound = SoundLoader.load('beep.wav')
btn = Button(text="Play Sound")
btn.bind(on_press=self.play_sound)
return btn
def play_sound(self, instance):
if self.sound:
self.sound.play()
print("🎵 Beep played!")
SoundApp().run()
Supports WAV, MP3, OGG. If it doesn't play, check if you installed ffpyplayer
:
pip install ffpyplayer
.
12. Using TextInput for User Input
Let users type things! TextInput
captures keyboard input — perfect for forms, chat, or creative
writing apps.
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.uix.button import Button
class InputApp(App):
def build(self):
layout = BoxLayout(orientation='vertical')
self.input = TextInput(hint_text="Type something amazing...")
self.label = Label(text="Your text will appear here")
btn = Button(text="Show Text")
btn.bind(on_press=self.show_text)
layout.add_widget(self.input)
layout.add_widget(btn)
layout.add_widget(self.label)
return layout
def show_text(self, instance):
self.label.text = f"You wrote: {self.input.text}"
InputApp().run()
Type in the box, click the button, and watch your words appear below. Instant feedback!
13. Drawing Custom Graphics with Canvas
Want to draw circles, lines, or psychedelic patterns? Use Kivy's canvas
instructions.
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics import Color, Ellipse, Line
class DrawingWidget(Widget):
def __init__(self, **kwargs):
super().__init__(**kwargs)
with self.canvas:
Color(1, 0, 0, 1) # Red
Ellipse(pos=(100, 100), size=(200, 200))
Color(0, 1, 0, 1) # Green
Line(circle=(200, 200, 100), width=3)
Line(points=[100, 100, 300, 300, 100, 300], width=2)
class CanvasApp(App):
def build(self):
return DrawingWidget()
CanvasApp().run()
This draws a red circle, a green ring, and a triangle. The canvas is your digital sketchpad.
14. Animating Widgets with Animation Class
Movement delights users. Kivy's Animation
class smoothly transitions properties like position, size,
or color.
from kivy.app import App
from kivy.uix.button import Button
from kivy.animation import Animation
from kivy.uix.boxlayout import BoxLayout
class AnimatedButton(Button):
def on_press(self):
anim = Animation(x=300, y=200, duration=1) + Animation(size=(200, 200), duration=1)
anim.start(self)
class AnimationApp(App):
def build(self):
layout = BoxLayout(padding=50)
btn = AnimatedButton(text="Animate Me!")
layout.add_widget(btn)
return layout
AnimationApp().run()
Click the button. Watch it glide and grow. Chain animations with +
for complex sequences.
15. Packaging Your App for Android, iOS, Windows, macOS, Linux
You built it — now share it with the world! Kivy works everywhere. Use Buildozer for Android, Kivy-ios for iOS, and PyInstaller for desktop.
# For Android (Linux/macOS required)
pip install buildozer
buildozer init
buildozer -v android debug
# For Windows (using PyInstaller)
pip install pyinstaller
pyinstaller --onefile --windowed --name "MyKivyApp" main.py
⚠️ Packaging is a deep topic. Start with desktop (PyInstaller) — it's easiest. Android requires Java, SDK, NDK. Don't panic — the Kivy docs have your back.
🎉 You Did It!
Congratulations! You've covered the ultra essentials of Kivy. From installation to animation to packaging — you're now equipped to build real, beautiful, cross-platform Python apps. Keep experimenting. Break things. Fix them. Share your creations. The Kivy community is rooting for you.
Now go make something amazing.