Learn Kivy - 10 Code Examples & CST Typing Practice Test
Kivy is an open-source Python framework for building cross-platform multitouch applications on Android, iOS, Windows, macOS, Linux, and Raspberry Pi. It provides a UI toolkit, gesture support, animations, layouts, and a declarative KV language for rapid app development.
Learn KIVY with Real Code Examples
Updated Nov 24, 2025
Code Sample Descriptions
Kivy Counter Example
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
class CounterApp(App):
def build(self):
self.count = 0
layout = BoxLayout(orientation='vertical', padding=20)
self.label = Label(text=f'Count: {self.count}', font_size=32)
inc_btn = Button(text='Increment', on_press=lambda x: self.increment())
layout.add_widget(self.label)
layout.add_widget(inc_btn)
return layout
def increment(self):
self.count += 1
self.label.text = f'Count: {self.count}'
if __name__ == '__main__':
CounterApp().run()
Demonstrates a simple counter layout using Kivy widgets for interactivity.
Kivy Todo List App
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.label import Label
class TodoApp(App):
def build(self):
self.tasks = []
self.layout = BoxLayout(orientation='vertical', padding=20)
self.input = TextInput(hint_text='Enter task')
self.layout.add_widget(self.input)
self.add_btn = Button(text='Add Task', on_press=lambda x: self.add_task())
self.layout.add_widget(self.add_btn)
self.task_box = BoxLayout(orientation='vertical')
self.layout.add_widget(self.task_box)
return self.layout
def add_task(self):
task = self.input.text.strip()
if task:
self.task_box.add_widget(Label(text=task))
self.input.text = ''
if __name__ == '__main__':
TodoApp().run()
A simple Todo app built with Kivy allowing users to add and view tasks.
Kivy Theme Switcher
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.graphics import Color, Rectangle
class ThemeApp(App):
def build(self):
self.dark = False
self.layout = BoxLayout()
self.button = Button(text='Switch Theme', on_press=lambda x: self.toggle_theme())
self.layout.add_widget(self.button)
self.update_bg()
return self.layout
def update_bg(self):
self.layout.canvas.before.clear()
with self.layout.canvas.before:
Color(0.1, 0.1, 0.1) if self.dark else Color(1, 1, 1)
Rectangle(size=self.layout.size, pos=self.layout.pos)
def toggle_theme(self):
self.dark = not self.dark
self.update_bg()
if __name__ == '__main__':
ThemeApp().run()
Demonstrates theme switching between light and dark using canvas colors.
Kivy Slider Example
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.slider import Slider
from kivy.uix.label import Label
class SliderApp(App):
def build(self):
self.layout = BoxLayout(orientation='vertical', padding=20)
self.label = Label(text='Value: 0')
self.slider = Slider(min=0, max=100, value=0)
self.slider.bind(value=self.on_value)
self.layout.add_widget(self.label)
self.layout.add_widget(self.slider)
return self.layout
def on_value(self, instance, value):
self.label.text = f'Value: {int(value)}'
if __name__ == '__main__':
SliderApp().run()
Shows how to use Slider widgets and display their values dynamically.
Kivy Login UI Example
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
class LoginApp(App):
def build(self):
layout = BoxLayout(orientation='vertical', padding=20)
self.username = TextInput(hint_text='Username')
self.password = TextInput(hint_text='Password', password=True)
btn = Button(text='Login', on_press=lambda x: self.login())
layout.add_widget(self.username)
layout.add_widget(self.password)
layout.add_widget(btn)
return layout
def login(self):
print(f'User: {self.username.text}')
if __name__ == '__main__':
LoginApp().run()
Creates a basic login interface with username and password fields.
Kivy Stopwatch Example
from kivy.app import App
from kivy.clock import Clock
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
class StopwatchApp(App):
def build(self):
self.time = 0
self.running = False
self.layout = BoxLayout(orientation='vertical', padding=20)
self.label = Label(text='0.0', font_size=32)
self.layout.add_widget(self.label)
self.layout.add_widget(Button(text='Start', on_press=lambda x: self.start()))
self.layout.add_widget(Button(text='Stop', on_press=lambda x: self.stop()))
self.layout.add_widget(Button(text='Reset', on_press=lambda x: self.reset()))
return self.layout
def update(self, dt):
self.time += dt
self.label.text = f'{self.time:.1f}'
def start(self):
if not self.running:
self.running = True
Clock.schedule_interval(self.update, 0.1)
def stop(self):
self.running = False
Clock.unschedule(self.update)
def reset(self):
self.time = 0
self.label.text = '0.0'
if __name__ == '__main__':
StopwatchApp().run()
A simple stopwatch that starts, stops, and resets using Clock scheduling.
Kivy Toggle Button Example
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.togglebutton import ToggleButton
class ToggleExample(App):
def build(self):
layout = BoxLayout(padding=20)
self.toggle = ToggleButton(text='OFF', on_press=self.toggle_state)
layout.add_widget(self.toggle)
return layout
def toggle_state(self, btn):
btn.text = 'ON' if btn.state == 'down' else 'OFF'
if __name__ == '__main__':
ToggleExample().run()
Demonstrates Kivy’s ToggleButton for state-based interaction.
Kivy Color Picker Example
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.colorpicker import ColorPicker
from kivy.graphics import Color, Rectangle
class ColorPickerApp(App):
def build(self):
self.layout = BoxLayout()
self.picker = ColorPicker()
self.picker.bind(color=self.on_color)
self.layout.add_widget(self.picker)
with self.layout.canvas.before:
self.bg = Color(1,1,1)
self.rect = Rectangle(size=self.layout.size, pos=self.layout.pos)
return self.layout
def on_color(self, instance, value):
self.bg.rgb = value[:3]
if __name__ == '__main__':
ColorPickerApp().run()
Uses Kivy’s ColorPicker widget to change background color dynamically.
Kivy Calculator Example
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
class Calculator(App):
def build(self):
self.result = TextInput(multiline=False, readonly=True)
layout = GridLayout(cols=4, spacing=5, padding=10)
layout.add_widget(self.result)
buttons = ['7','8','9','/','4','5','6','*','1','2','3','-','0','.','=','+']
for b in buttons:
btn = Button(text=b, on_press=self.on_button)
layout.add_widget(btn)
return layout
def on_button(self, instance):
if instance.text == '=':
try:
self.result.text = str(eval(self.result.text))
except:
self.result.text = 'Error'
else:
self.result.text += instance.text
if __name__ == '__main__':
Calculator().run()
A simple calculator UI built using Kivy layout widgets.
Kivy Image Viewer Example
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.image import Image
from kivy.uix.button import Button
class ImageViewer(App):
def build(self):
self.images = ['1.png','2.png','3.png']
self.index = 0
self.layout = BoxLayout(orientation='vertical')
self.img = Image(source=self.images[self.index])
btn_next = Button(text='Next', on_press=lambda x: self.next_img())
self.layout.add_widget(self.img)
self.layout.add_widget(btn_next)
return self.layout
def next_img(self):
self.index = (self.index + 1) % len(self.images)
self.img.source = self.images[self.index]
if __name__ == '__main__':
ImageViewer().run()
Displays and switches between images using buttons in Kivy.
Frequently Asked Questions about Kivy
What is Kivy?
Kivy is an open-source Python framework for building cross-platform multitouch applications on Android, iOS, Windows, macOS, Linux, and Raspberry Pi. It provides a UI toolkit, gesture support, animations, layouts, and a declarative KV language for rapid app development.
What are the primary use cases for Kivy?
Mobile apps (Android/iOS). Touchscreen kiosks and dashboards. Cross-platform GUI apps in Python. Prototyping and educational tools. Raspberry Pi and hardware interfaces
What are the strengths of Kivy?
Python-based - easy for Python developers. Cross-platform mobile + desktop. Lightweight and flexible. Multitouch and gesture support. Great for rapid development
What are the limitations of Kivy?
Not ideal for heavy 3D or gaming. UI performance can lag vs native frameworks. iOS builds require complex setup. Smaller ecosystem compared to Flutter/React Native. Not suitable for high-performance animations
How can I practice Kivy typing speed?
CodeSpeedTest offers 10+ real Kivy code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.