Kivy is a platform independent GUI tool in Python. As it can be run on Android, IOS, linux and Windows etc. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktops applications.
The Canvas is the root object used for drawing by a Widget . A kivy canvas is not the place where you paint. The major problems I had at the beginning with the canvas were due to its name. Particularly considering all the buzz about HTML5 canvas. I initially think that the canvas is the paint But canvas is basically a container of instructions.
To use Canvas you must have to import:
from kivy.graphics import Rectangle, Color
Note: Each Widget in Kivy already has a Canvas by default. When you create a widget, you can create all the instructions needed for drawing. If self is your current widget. The instructions Color and Rectangle are automatically added to the canvas object and will be used when the window is drawn.
Basic Approach -> import kivy -> import kivy App -> import widget -> import Canvas i.e.: from kivy.graphics import Rectangle, Color -> set minimum version(optional) -> Extend the Widget class -> Create the App Class -> return a Widget -> Run an instance of the class
Implementation of the Approach –
You can also use any other widget in canvas. In the below example we will show how to add image and change its color.
To change the color just change the canvas color that will change the image color.
Original image used in App is:
Image in Canvas:
Kivy drawing instructions are not automatically relative to the widgets’ position or size. You, therefore, you need to consider these factors when drawing. In order to make your drawing instructions relative to the widget, the instructions need either to be declared in the KvLang or bound to pos and size changes.
- Python | Canvas in Kivy using .kv file
- Python | Line (Canvas) in kivy
- Circular (Oval like) button using canvas in kivy (using .kv file)
- Python | Kivy .kv File
- Introduction to Kivy ; A Cross-platform Python Framework
- Python | Ellipse (different polygons) in Kivy
- Python | Scrollview widget in kivy
- Python | Carousel Widget In Kivy
- Python | Float Layout in Kivy
- Python | Make a simple window using kivy
- GridLayouts in Kivy | Python
- Python | Working with buttons in Kivy
- Python | Add Label to a kivy window
- Python | Button Action in Kivy
- Python | Window size Adjustment in Kivy
- Python | BoxLayout widget in Kivy
- Python | Slider widget in Kivy
- Python | Checkbox widget in Kivy
- Python | Multiple Sliders widgets Controlling Background Screen or WindowColor in Kivy
- Python | AnchorLayout in Kivy using .kv file
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.