Configs2026年4月15日·1 分钟阅读

Kivy — Open-Source Python Framework for Cross-Platform Apps with a Single Codebase

Kivy lets you write touch-friendly cross-platform apps entirely in Python. One codebase runs on iOS, Android, Windows, macOS, Linux, and Raspberry Pi — the Python way to build mobile and kiosk UIs.

Introduction

Kivy was born in 2011 as the "Python answer" to cross-platform UI. With Kivy, a single Python codebase runs on Android, iOS, Windows, macOS, Linux, and Raspberry Pi, with true multi-touch support. The graphics layer uses OpenGL ES 2, so rendering is hardware-accelerated everywhere.

With over 18,000 GitHub stars, Kivy powers many Python-centric mobile apps, kiosk UIs, and educational projects. For hobbyists who know Python but don't want to learn Swift/Kotlin/Java/Flutter/Dart, Kivy remains the go-to choice.

What Kivy Does

Kivy apps are Python classes extending App. UI builds from Widget subclasses. The kv declarative language lets you separate layout from logic (similar to QML). Properties + events are first-class — reactive bindings rebuild UI when values change. Buildozer packages Android APKs; python-for-ios bundles into an Xcode project.

Architecture Overview

[Your Python code + .kv files]
      |
[Kivy App]
   Widget tree (Label, Button, Grid, Scatter, ...)
      |
[Properties + Events]
   reactive bindings, on_press, on_touch_down
      |
[Graphics layer]
   OpenGL ES 2 draw instructions
   per-widget canvas (VBOs, shaders)
      |
[Platform window providers]
   SDL2 (desktop), Android (python-for-android), iOS (kivy-ios)
      |
[Touch input]
   multitouch-first, mouse emulated as single touch
      |
[Buildozer / kivy-ios / PyInstaller]
   produces .apk / .ipa / .exe

Self-Hosting & Configuration

# app.py — with .kv file for layout
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import NumericProperty

class Counter(BoxLayout):
    value = NumericProperty(0)

    def increment(self):
        self.value += 1

class CounterApp(App):
    def build(self):
        return Counter()

if __name__ == "__main__":
    CounterApp().run()
# counter.kv (auto-discovered by matching class name)
<Counter>:
    orientation: "vertical"
    padding: 20
    spacing: 10
    Label:
        text: str(root.value)
        font_size: 48
    Button:
        text: "Increment"
        on_press: root.increment()
# Package for Android (requires Linux or WSL)
pip install buildozer
buildozer init              # generate buildozer.spec
buildozer android debug     # build APK
buildozer android deploy run  # push to connected device

# Package for iOS (requires macOS + Xcode)
pip install kivy-ios
toolchain build python3 kivy
toolchain create MyApp /path/to/app
# Open MyApp.xcodeproj, build + archive

Key Features

  • Pure Python — no Swift/Kotlin/Dart, just Python + optional .kv
  • Multi-touch first — designed from day one for mobile + tablets
  • Cross-platform — Android, iOS, desktop, Raspberry Pi (same code)
  • Kv language — declarative UI + bindings separate from logic
  • OpenGL ES 2 renderer — hardware-accelerated, smooth 60 FPS
  • Kivy Garden — community widget ecosystem (charts, maps, custom inputs)
  • Kivy MD — Material Design components on top of Kivy
  • Buildozer — one-command Android packaging from Linux

Comparison with Similar Tools

Feature Kivy BeeWare (Toga) Flutter React Native Tkinter
Language Python Python Dart JS/TS Python
Custom rendering OpenGL ES Native widgets Skia Native views Tk widgets
Mobile Yes (mature) Yes (earlier) Yes (best) Yes No
Native look No (custom) Yes Themable Yes OS-native
Desktop Yes Yes Yes Limited Yes
Best For Python mobile apps + kiosks Python + native widgets Production-quality apps JS devs Desktop Python GUIs

FAQ

Q: Kivy vs BeeWare Toga? A: Kivy has its own OpenGL-based rendering (consistent look across platforms); BeeWare wraps native OS widgets (native look + feel). Pick Kivy if you want a game-like / custom UI; BeeWare for traditional native apps.

Q: Is performance good enough for production? A: Yes for UIs and games with moderate complexity. Large lists require virtualization (RecycleView). Heavy animations should use the Kivy animation system rather than manual tweens.

Q: Can Kivy apps look native? A: By default no — Kivy has its own look. Use KivyMD for Material Design-styled widgets that fit Android, or roll your own theme.

Q: Is buildozer painful? A: Initial setup is finicky (needs Android SDK, NDK, recipes). Once working, subsequent builds are reliable. Many users run buildozer inside Docker for reproducibility.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产