kivy
  1. kivy-drawing-shapes-and-lines

Drawing Shapes and Lines - (Kivy Graphics and Drawing)

Description

Kivy is an open-source Python library, which is used for the creation of multi-touch applications. It comes with powerful tools to create UI designs, build games, and more. One of the key features of Kivy is its ability to create and draw graphics in the application's UI.

This tutorial will cover the basics of drawing shapes and lines in Kivy.

Syntax

To create and draw a shape or line in Kivy, you will need to import the graphics module and use the appropriate classes and functions. Here is the basic syntax:

from kivy.graphics import *
 
with your_widget.canvas:
    # create a shape or line

Example

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics import *

class MyWidget(Widget):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
 
        with self.canvas:
            # create a line
            Color(1, 0, 0, 1) # red color
            Line(points=[100, 100, 200, 200], width=2)

            # create a rectangle
            Color(0, 1, 0, 1) # green color
            Rectangle(pos=(300, 100), size=(100, 50))

            # create a circle
            Color(0, 0, 1, 1) # blue color
            Ellipse(pos=(450, 100), size=(50, 50))

class MyPaintApp(App):
    def build(self):
        return MyWidget()

if __name__ == '__main__':
    MyPaintApp().run()

In this example, we create a MyWidget class that inherits from the Widget class in Kivy. We then add the shapes and lines to the widget's canvas in the __init__ method.

We create a red line using the Line class and green rectangle using the Rectangle class. We also create a blue circle using the Ellipse class.

Finally, we create a MyPaintApp class that inherits from the App class in Kivy. We then return an instance of the MyWidget class from the build method.

Output

kivy_shapes

Explanation

In the example, we first import the necessary modules to our program. With the help of kivy.graphics module we can import lines, shapes and other objects that we need to make designs of our choice. Then a MyWidget class is created which is inherited from the Widget class of Kivy.

We have defined three shapes in this example. The first is a red line from the points (100,100) and (200,200) with the help of the Line class. We have used the Color function to set the color of the shape.

The second shape is a green rectangle having position pos = (300,100) and size size = (100, 50) using the Rectangle class.

The third shape is a blue circle with its position as (450, 100) and size as (50, 50) using the Ellipse class. We have used the Color function to set the color of the shape.

Finally, we have called the MyPaintApp and run the method to open the widget window.

Use

Kivy provides a flexible and powerful graphics API for creating and drawing shapes and lines in an application's user interface. The kivy graphics module gives developers the ability to create complex designs and interactive applications with ease.

Important Points

  • The kivy.graphics module allows us to create and modify graphics in Kivy.
  • We can import various shapes such as ellipse, rectangle, line and much more with the help of relevant classes of the kivy.graphics module.
  • We can define different colors for the shapes using the Color class.
  • with your_widget.canvas: is used to attach shapes or points to the widget being created.

Summary

  • Kivy provides a wide range of graphics tools to developers to create and draw shapes and lines in their application's user interface.
  • With the help of the Kivy graphics module, we can import and define different kinds of shapes.
  • Different shades and colors can be used using the Color function.
  • With the help of with your_widget.canvas: we can render and attach shapes or lines to the widget.
Published on: