Welcome to the Python Tutorial on Kivy Apps! In this lesson, we'll dive into the world of creating interactive applications using Kivy, a powerful open-source Python library. By the end of this tutorial, you'll be able to build your own mobile and desktop applications. 📝 Note: Kivy supports both Python 2.x and 3.x.
Kivy is a cross-platform framework for developing applications that can run on various platforms such as Linux, macOS, Windows, Android, and iOS. It's built on top of OpenGL ES and provides a high-level interface for creating graphics, handling user input, and more.
To install Kivy, you can use pip, which is a package manager for Python. Here's the command to install Kivy:
pip install kivyLet's create a simple Kivy application that displays a message on the screen.
from kivy.app import App
from kivy.uix.label import Label
class MyApp(App):
def build(self):
return Label(text="Hello, World!")
if __name__ == "__main__":
MyApp().run()Save this code as my_app.py, and run it using the following command:
python my_app.pyYou should see a window with the text "Hello, World!" displayed. 🎉
Every Kivy application consists of layers, which are stacked on top of each other. Each layer contains widgets, which are the building blocks of Kivy applications. There are various types of widgets, such as:
Label: Displays textButton: Creates clickable buttonsTextInput: Allows users to input textImage: Displays imagesBoxLayout: Organizes widgets in a horizontal or vertical layoutGridLayout: Organizes widgets in a gridNow, let's create a simple calculator app using Kivy.
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
class Calculator(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.numbers = ['7', '8', '9', '4', '5', '6', '1', '2', '3', '0']
self.operations = ['+', '-', '*', '/']
self.result = TextInput(multiline=False)
self.build_ui()
def build_ui(self):
row1 = BoxLayout(direction='horizontal')
for number in self.numbers[0:3]:
button = Button(text=number)
button.bind(on_press=self.on_number_press)
row1.add_widget(button)
row2 = BoxLayout(direction='horizontal')
for number in self.numbers[3:6]:
button = Button(text=number)
button.bind(on_press=self.on_number_press)
row2.add_widget(button)
row3 = BoxLayout(direction='horizontal')
for number in self.numbers[6:]:
button = Button(text=number)
button.bind(on_press=self.on_number_press)
row3.add_widget(button)
operation_buttons = [Button(text=operation) for operation in self.operations]
zero_button = Button(text='0')
zero_button.bind(on_press=self.on_number_press)
dot_button = Button(text='.')
dot_button.bind(on_press=self.on_dot_press)
equals_button = Button(text='=')
equals_button.bind(on_press=self.on_equals_press)
for button in operation_buttons:
row1.add_widget(button)
row1.add_widget(zero_button)
row1.add_widget(dot_button)
row1.add_widget(equals_button)
self.add_widget(row1)
self.add_widget(row2)
self.add_widget(row3)
self.add_widget(self.result)
def on_number_press(self, instance):
current_text = self.result.text
if len(current_text) > 0 and current_text[-1] == ' ':
current_text = current_text[:-1]
self.result.text = current_text + ' ' + instance.text
def on_dot_press(self, instance):
current_text = self.result.text
if '.' not in current_text:
self.result.text = current_text + '.'
def on_equals_press(self, instance):
try:
result = eval(self.result.text.replace(' ', ''))
self.result.text = str(result)
except ZeroDivisionError:
self.result.text = "Error: Division by zero"
class CalculatorApp(App):
def build(self):
return Calculator()
if __name__ == "__main__":
CalculatorApp().run()Save this code as calculator.py, and run it using the following command:
python calculator.pyYou should see a calculator app with number buttons, operation buttons, and a result text input.