Library Guide

Kivy

GPU-accelerated, multi-touch toolkit for mobile, desktop and custom game-like UIs.

Mobile + desktop GPU / OpenGL Multi-touch

What Is Kivy?

Kivy is an open-source Python framework for building multitouch applications. Unlike tkinter or wxPython, Kivy does not use native OS widgets โ€” it renders everything with OpenGL ES 2, giving a consistent look across Windows, macOS, Linux, Android, and iOS. This makes it the primary choice for Python developers who need mobile deployment or touchscreen kiosks.

Kivy was created by the Kivy organization and is used in education, museums, industrial HMIs, and indie games. UIs can be built in pure Python or split between Python logic and the declarative KV language (similar to QML).

Architecture & Core Concepts

  • App โ€” subclass kivy.app.App; implement build() to return the root widget.
  • Widget tree โ€” layouts (BoxLayout, GridLayout, FloatLayout) contain child widgets.
  • Properties โ€” widget attributes (text, pos, size) are reactive; bind with widget.bind(on_press=callback).
  • Clock โ€” Clock.schedule_interval() for animations and game loops.
  • KV language โ€” separate .kv files define layout and styling declaratively.
  • Input โ€” multi-touch, gestures, accelerometer (on mobile) built in.

Mobile packaging toolchain

ToolTarget
BuildozerAndroid APK/AAB (Linux build recommended)
python-for-androidLow-level Android build recipes
kivy-iosiOS builds (requires macOS + Xcode)

Installation

shell
pip install kivy

# Optional extras:
pip install kivy[base,media]

# For Android builds (on Linux):
pip install buildozer

Example 1 โ€” Simple button app

python
from kivy.app import App
from kivy.uix.button import Button

class DemoApp(App):
    def build(self):
        btn = Button(text="Tap me", font_size=28)
        btn.bind(on_press=lambda *_: print("tapped"))
        return btn

DemoApp().run()

Example 2 โ€” Vertical layout with multiple widgets

python
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.button import Button

class CounterApp(App):
    def build(self):
        self.count = 0
        layout = BoxLayout(orientation="vertical", padding=20, spacing=10)
        self.label = Label(text="0", font_size=48)
        btn = Button(text="Add", size_hint=(1, 0.2))
        btn.bind(on_press=self.add)
        layout.add_widget(self.label)
        layout.add_widget(btn)
        return layout
    def add(self, *_):
        self.count += 1
        self.label.text = str(self.count)

CounterApp().run()

Example 3 โ€” KV language (demo.kv + main.py)

Save layout in a .kv file named after your App class (DemoApp โ†’ demo.kv):

demo.kv
<DemoApp>:
    BoxLayout:
        orientation: 'vertical'
        padding: 30
        Label:
            text: 'Hello from KV'
            font_size: 32
        Button:
            text: 'Quit'
            on_press: app.stop()
main.py
from kivy.app import App
class DemoApp(App): pass
DemoApp().run()

Practical advice

  • Use size_hint and pos_hint instead of fixed pixel sizes for responsive layouts.
  • Test on target devices early โ€” desktop behavior differs from touch screens.
  • KivyMD (Material Design) extends Kivy with polished Android-style components.
  • Buildozer requires Linux for Android; plan your CI/build environment accordingly.
  • Use ScreenManager for multi-screen apps (login โ†’ main โ†’ settings).

Real-world use cases

  • Android/iOS apps from a single Python codebase
  • Museum kiosks and interactive exhibits
  • Industrial touch panels and HMIs
  • 2D games and educational simulations
  • Raspberry Pi touchscreen projects

โœ“ Strengths

  • One codebase โ†’ Android, iOS, desktop
  • GPU-accelerated, smooth animations
  • Multi-touch & gestures native
  • Great for games & custom UIs
  • MIT license, active community

โœ— Weaknesses

  • Non-native look (custom-drawn)
  • Mobile packaging can be fiddly
  • Different paradigm (KV language) to learn
  • Smaller job market than Qt/web
Use it when

You need a mobile app, a touchscreen kiosk, or a game-like custom interface from Python. Avoid when native desktop appearance or simple forms are the priority.