Table of contents
1.
Introduction
2.
Canvas
2.1.
Implementation of the approach
2.2.
Output
2.3.
Change Color of screen
2.4.
Output
3.
Frequently Asked Question
3.1.
What exactly are RGBA color codes?
3.2.
Is Kivy free?
3.3.
In Kivy, what is a canvas?
3.4.
What exactly is the KIVY scatter?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

Canvas in Kivy using .kv file

Author SHIVANGI MALL
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Kivy is a Python GUI development library that runs on iOS, Android, Windows, OS X, and GNU/Linux platforms. It helps developers create apps with cutting-edge multi-touch user interfaces. Kivy's basic concept allows developers to create an app once and deploy it across all devices, allowing for reusable and deployable code as well as rapid and easy interaction design and prototyping.

In this article, with the help of a few examples, we'll look at Canvas in Kivy using .kv file. This post assumes you've done some Python programming before. If that is not the case, you can return here after reading our Basics of Python article.

Canvas

The Canvas is the root object used for drawing by a Widget. A kivy canvas is not the place where you paint. By default, every Widget in Kivy comes with a Canvas. When you make a widget, you can include all of the drawing instructions if self is the widget you're currently using. When the window is drawn, the instructions Color and Rectangle are immediately appended to the canvas object.


Import the following module to use canvas:

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 needed
-> return a Widget
-> Run an instance of the class

Implementation of the approach

Now we'll look at a sample python application code that uses Kivy to show how the approach described above for canvas works. .kv file stores source code in the Kivy syntax, which include rule definitions, a root widget, dynamic class definitions, and templates. Each Widget in Kivy already has a Canvas by default. When you create a widget, you can create all the instructions needed for drawing. 

main.py file

# to import kivy module
import kivy
   
# this restricts the version i.e
# below this kivy version, we cannot
# use the app
kivy.require("1.9.1")

# app:always used to refer the instance of your application
from kivy.app import App

# we are importing from graphics module
# Rectangle and Color as they are
# used for the basic building of canvas.
from kivy.graphics import Rectangle, Color

# Button behavior is provided by ButtonBehavior mixin class
from kivy.uix.button import ButtonBehavior

# The Label widget is used for rendering text.
from kivy.uix.label import Label

# class in which we create the canvas
class CanvasWidget(ButtonBehavior, Label):
    pass

# to create the App Class
class CanvasApp(App):
    def build(self):
        return CanvasWidget()

# run the App
CanvasApp().run()


.kv file

# .kv file of canvas

<CanvasWidget@Widget>

# to create canvas
canvas:
Color:
rgba: 250, 250, 0, 1 # Yellow

# size and position of Canvas
Rectangle:
pos: self.pos
size: self.size

Output

Change Color of screen

Now we'll look at a sample python application code that uses Kivy to show how the color of the screen changes. We have changed the color using python statements as a value expression for property and on_property attributes. Self keyword refers to the current widget context. 

main.py file


# to import kivy module
import kivy
   
# this restricts the version i.e
# below this kivy version, we cannot
# use the app
kivy.require("1.9.1")
   
# app:always used to refers the instance of your application
from kivy.app import App

#  we are importing from graphics module
# Rectangle and Color as they are
# used for basic building of canvas.
from kivy.graphics import Rectangle, Color

# Button behavior is provided by ButtonBehavior mixin class
from kivy.uix.button import ButtonBehavior

# The Label widget is used for rendering text.
from kivy.uix.label import Label

# class in which we create the canvas
class CanvasWidget(ButtonBehavior, Label):
    pass

# to create the App Class
class CanvasApp(App):
    def build(self):
        return CanvasWidget()

# run the App
CanvasApp().run()

.kv file

# .kv file of canvas
<CanvasWidget>

# Creating Canvas
canvas:

# Color is skyblue if button is pressed,
# otherwise color is green
Color:
rgb: (0.4, 1, 0, 1) if self.state == 'normal' else (0.5, 0.9, 1, 1)

# to create rounded rectangle canvas
RoundedRectangle:
size: self.size
pos: self.pos

# use this if you want smooth corners for your button
radius: 250, 250, 250, 250
# Print the text when touched or button pressed

on_release:

print("I have been clicked")

Output

 

Frequently Asked Question

What exactly are RGBA color codes?

RGBA color values are an extension of RGB color values with an alpha channel that indicates a color's opacity. The RGBA color value is defined using the following syntax: rgba (red, green, blue, alpha). The alpha value ranges from 0.0 to 1.0

 

Is Kivy free?

Kivy is a free and open-source Python framework for developing mobile apps and application software with a natural user interface (NUI).

 

In Kivy, what is a canvas?

The Canvas is the root object that a Widget can use to draw on. By default, every Widget in Kivy has a Canvas. You may create all of the drawing instructions while constructing a widget.

 

What exactly is the KIVY scatter?

Scatter is a multitouch technology that allows you to translate, rotate, and scale interactive widgets with two or more fingers. Scatter has a matrix transformation of its own.

Conclusion

We have studied Canvas in Kivy using .kv file in this article. You may learn and use a variety of Python frameworks and tools for application development.

We hope that this blog has helped you learn more about Canvas in kivy and if you're interested in learning more, see our articles on  Application DevelopmentModules & Packages in Python, and Best Python ProjectsApplications Development Using Python. Do upvote our blog to help other ninjas grow.


Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.!

Happy Reading!

 

 

Live masterclass