Kivy is an open-source Python library for developing multi-touch applications. It can run on multiple platforms including Windows, macOS, Linux, iOS, and Android. Kivy allows you to create interactive user interfaces and build cross-platform applications using a single codebase. In this tutorial, we will cover the basics of Kivy to help you get started with developing your own applications.
- Installing Kivy
To begin with, you need to install Kivy on your system. You can install Kivy using pip by running the following command in your terminal:
pip install kivy
You can also install additional dependencies for specific features using the following command:
pip install kivy[base]
- Creating a Kivy Application
To create a Kivy application, you need to create a Python script and import the Kivy library. You can start by creating a simple script with the following code:
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()
This script creates a simple Kivy application with a label displaying "Hello, World!". The MyApp
class inherits from the App
class, and the build
method returns a Label
widget with the text "Hello, World!". Finally, we run the application with MyApp().run()
.
- Running the Kivy Application
To run the Kivy application, save the script to a file (e.g.,main.py
) and run it using the Python interpreter:
python main.py
This will launch the Kivy application with the label displaying "Hello, World!".
- Understanding Widgets
Widgets are the building blocks of Kivy applications. They represent visual elements such as buttons, labels, text inputs, and more. Widgets can be organized in a tree structure, with each widget being a child of another widget. Here are some common widgets in Kivy:
Label
: displays textButton
: a clickable buttonTextInput
: allows users to input textBoxLayout
: organizes widgets in a horizontal or vertical layoutGridLayout
: organizes widgets in a grid layoutImage
: displays an imageScrollView
: allows scrolling of content
- Organizing Widgets
You can organize widgets using layout classes such asBoxLayout
,GridLayout
, andFloatLayout
. Layouts define how widgets are positioned and sized within a window. For example, here is how you can organize widgets using aBoxLayout
:
from kivy.uix.boxlayout import BoxLayout
class MyWidget(BoxLayout):
def __init__(self, **kwargs):
super(MyWidget, self).__init__(**kwargs)
self.orientation = 'vertical'
label1 = Label(text='Label 1')
label2 = Label(text='Label 2')
self.add_widget(label1)
self.add_widget(label2)
In this example, we create a custom widget MyWidget
that inherits from BoxLayout
. We set the orientation of the BoxLayout
to vertical and add two Label
widgets to it.
- Responding to User Input
Kivy allows you to bind events to widgets and respond to user input. You can handle events such as button clicks, text input changes, mouse movements, and more. Here is an example of how you can handle a button click event:
from kivy.uix.button import Button
class MyButton(Button):
def on_press(self):
print('Button clicked!')
button = MyButton(text='Click Me')
button.bind(on_press=lambda instance: button.on_press())
In this example, we create a custom button MyButton
that inherits from Button
. We define an on_press
method that is called when the button is pressed. We then bind the on_press
method to the on_press
event of the button.
- Adding Graphics
Kivy supports drawing graphics using theCanvas
class. You can draw shapes, lines, and text on a widget’s canvas. Here is an example of how you can draw a rectangle on a widget’s canvas:
from kivy.graphics import Rectangle, Color
class MyWidget(Widget):
def __init__(self, **kwargs):
super(MyWidget, self).__init__(**kwargs)
with self.canvas:
Color(1, 0, 0)
Rectangle(pos=self.pos, size=self.size)
In this example, we create a custom widget MyWidget
that inherits from Widget
. We use the Canvas
class to draw a red rectangle on the widget’s canvas.
- Building a Complete Application
Now that you have learned the basics of Kivy, you can start building a complete application. You can create multiple screens, handle user input, load data from external sources, and more. Here is an example of a simple calculator application using Kivy:
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
class CalculatorApp(GridLayout):
def __init__(self, **kwargs):
super(CalculatorApp, self).__init__(**kwargs)
self.cols = 4
self.display = TextInput(multiline=False)
self.add_widget(self.display)
buttons = [
'7', '8', '9', '/',
'4', '5', '6', '*',
'1', '2', '3', '-',
'.', '0', '=', '+'
]
for button in buttons:
self.add_widget(Button(text=button))
if __name__ == '__main__':
from kivy.app import App
class CalculatorAppApp(App):
def build(self):
return CalculatorApp()
CalculatorAppApp().run()
This script creates a simple calculator application with a text input field for displaying the result and buttons for inputting numbers and operators.
- Conclusion
In this tutorial, you have learned the basics of Kivy and how to create interactive user interfaces and build cross-platform applications. Kivy provides powerful tools for developing applications with rich graphical user interfaces. You can explore more features and capabilities of Kivy by referring to the official documentation and examples. Happy coding!
Access COMPLETE COURSES on Python here: https://academy.zenva.com/product/python-programming-mini-degree/?utm_campaign=youtube_comment&utm_medium=youtube&utm_content=youtube_2020_minicoursekivyin60mins
Just got a subscriber
This is the best tut on kivy I've seen on yb. Very simple and easy 👍…. please do you have tut on building exams app in kivy with login system?
Just wow….a widget as the root? We've never heard of such wizardry!!#MindBlowing
How about to make video about adding in-app purchases by Kivy and uploading then to Google Play ?
Can I use Kivy in 2023?
Good video.cuz I think you want to place the first bricks in our brain carefully and in the right place so that we can confidently build our big structure on it. Thankful. Continue in the same way.
Using kivy 2.1.0: At minute 50 approx where the Float Layout is given pos and size, the buttons DISAPPEAR:
great toturial!.thoughi have hard time keeping up with screen. i often pause for 5 seconds and look away and play once more. any way, it's a great toturial thanks!
Thank you for the excellent video.
And how to use several of your own layouts?
Something like this:
—
<myBoxContainer>:
Button:
text:'7'
size_hint:(1, .5)
pos_hint:{"center_x":0.5, "center_y":0.5}
<myGridContainer>:
Button:
text:'9'
pos_hint:{"center_x":0.7, "center_y":0.7}
Excellent video
I couldn't find next tutorial of Label
Did you publish the video?
can someone pls tell me how to do a .kv file in pycharm.
How we can access a video from online platform into our kivy applications please reply to me
Good course Zenva
Could you briefly explain why you took the If _name_ == _main__: in. Sry if the question seems stupid I'm still a beginner but would the execution of the sub class not be enough and where exactly is __name_ defined, is this a special variable.
this is the best kivy tutorial on youtube i l😍ve it … iwish y make more advance kivy tutorial in the future
Only suspicious people choose visual studio code instead of pycharm
I confuse with this tutorial, why dont use android studio it faster then this kivy
Thank you very much for making this video🙏. It helped me to get introduced to Kivy😊. Respect!!
Would this run the same way on pycharm?