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.
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.
To use Canvas you must have to import:
from kivy.graphics import Rectangle, Color
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 -> create the .kv file: -> create the canvas -> Add action/callback if needeed -> return a Widget -> Run an instance of the class
1) Creating A simple canvas:
Now How can we change the color of canvas by any action so below is the example of in which by clicking the color of screen changes.
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.
- Circular (Oval like) button using canvas in kivy (using .kv file)
- Python | Canvas in kivy
- Python | Line (Canvas) in kivy
- Python | Kivy .kv File
- Python | ScreenManager in Kivy using .kv file
- Python | PageLayout in Kivy using .kv file
- Python | Animation in Kivy using .kv file
- Python | StackLayout in Kivy using .kv file
- Python | FloatLayout in Kivy using .kv file
- Python | AnchorLayout in Kivy using .kv file
- Python | File chooser in kivy
- Python | Accordion in kivy using .kv file
- Python | TextInput in kivy using .kv file
- Python | Spinner widget in Kivy using .kv file
- Python | Popup widget 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.