Calculator Example - Kivy Typing CST Test
Loading…
Calculator Example — Kivy Code
A simple calculator UI built using Kivy layout widgets.
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()Kivy Language Guide
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.
Primary Use Cases
- ▸Mobile apps (Android/iOS)
- ▸Touchscreen kiosks and dashboards
- ▸Cross-platform GUI apps in Python
- ▸Prototyping and educational tools
- ▸Raspberry Pi and hardware interfaces
Notable Features
- ▸KV language for declarative UI
- ▸GPU-accelerated rendering
- ▸Multitouch gesture framework
- ▸Cross-platform build tools
- ▸Rich widget and layout system
Origin & Creator
Kivy was created by Mathieu Virbel and the Kivy Organization, evolving from the PyMT project and released publicly in 2011 as a modern Python GUI + mobile app framework.
Industrial Note
Kivy is especially loved in education, research environments, rapid prototyping, Raspberry Pi projects, and Python-first development teams requiring cross-platform touch UIs.