Table of contents
1.
Introduction
2.
Canvas
3.
Line Widget
4.
Implementation
4.1.
Python Code
4.2.
Commands to run Code
4.3.
Output
5.
Lines Extended Demo
5.1.
Code 1 (Python)
5.2.
Code 2 (Kivy)
5.3.
Output
6.
Frequently Asked Questions
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

Line(Canvas) Widget in Kivy

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The Kivy is a description language for user interfaces and interactions. In Kivy, a Widget is the fundamental component of a GUI interface. It comes with a Canvas that you may use to draw on the screen. 

Here we'll focus on the Line Widget of Kivy, which is one of the Vertex instructions of Canvas.

Canvas

A Canvas is used to render a Widgets graphical representation, which may be viewed as a limitless painting board or as a set of drawing instructions. You can apply (add) a variety of instructions to your canvas, but there are two primary variations:

  • context instructions
  • vertex instructions

Context instructions do not draw anything, but they affect the vertex instructions' outcomes.

We can use the Vertex instructions to draw basic shapes on a widget. The Line is also a Vertex Canvas instruction.

Line Widget

As discussed, Line Widget is a Vertex Canvas Instruction. We can use this Widget to create a simple drawing application. We will draw a simple application to show the functioning of the line widget then we will see the demo of using the Extended Line to draw patterns such as circles, ellipses, and rectangles.

We need to import several components from kivy.graphics before we can start drawing on the screen.

from kivy.app import App
from kivy.graphics import Rectangle
from kivy.graphics import Color
from kivy.uix.widget import Widget
You can also try this code with Online Python Compiler
Run Code

To draw a line, we first need to import the Line from kivy.graphics.

from kivy.graphics import Line
You can also try this code with Online Python Compiler
Run Code

Implementation

Here's a basic demonstration of using the Line widget in a Kivy application. After writing the code in an editor, save the file using the .py extension.

Filename: example.py

Python Code

from kivy.app import App
from kivy.graphics import Rectangle
from kivy.graphics import Color
from kivy.uix.widget import Widget
from kivy.graphics import Line

class Touch(Widget):
    def __init__(self, **kwargs):
        super(Touch, self).__init__(**kwargs)

        with self.canvas:
            Line(points=(20,30,300,400,60,300))
            Color(1,0,0,0.5, mode="rgba")
            self.rect = Rectangle(pos=(0,0), size=(25,25))

    def on_touch_down(self, touch):
        self.rect.pos = touch.pos

    def on_touch_move(self, touch):
        self.rect.pos = touch.pos

class DrawingApp(App):
 def build(self):
 return Touch()
DrawingApp().run()
You can also try this code with Online Python Compiler
Run Code

Commands to run Code

To run our program, consider the following steps:

Step 1: Open the Anaconda Prompt and enter the correct path to our kivy programme.

Step 2: Use the following command and press Enter to run the programme.

Python file_name.py (Example: example.py)

Output

Here is the output of our simple application.

Lines Extended Demo

This demonstrates how to draw circles, ellipses, and rectangles using the extended line drawing methods. On the screen, you will see a static display of labeled shapes.
Filename: Line.py

Code 1 (Python)

import kivy 
from kivy.app import App 
#The GridLayout organises children in a matrix.
from kivy.uix.gridlayout import GridLayout
from kivy.uix.widget import Widget
from kivy.lang import Builder

# Designate Our .kv design file 
Builder.load_file('line.kv')

 
# Classes form Different types of line as  widgets
 
class LineEllipseA(Widget):
    pass
 
class LineEllipseB(Widget):
    pass
 
class LineEllipseC(Widget):
    pass
 
class LineCircleA(Widget):
    pass
 
class LineCircleB(Widget):
    pass
 
class LineCircleC(Widget):
    pass
 
class LineCircleD(Widget):
    pass
 
class LineRectangle(Widget):
    pass
 
class LineBezier(Widget):
    pass
 
 
# Creating the App class
class LineExtendedApp(App):
    def build(self):
 
        # Assign the number of column, spacing and padding
        root = GridLayout(cols = 2, padding = 50, spacing = 50)
 
        # Adding the widgets
        root.add_widget(LineEllipseA())
        root.add_widget(LineEllipseB())
        root.add_widget(LineEllipseC())
        root.add_widget(LineCircleA())
        root.add_widget(LineCircleB())
        root.add_widget(LineCircleC())
        root.add_widget(LineCircleD())
        root.add_widget(LineRectangle())
        root.add_widget(LineBezier())
        return root

if __name__ == '__main__':
    LineExtendedApp().run()
You can also try this code with Online Python Compiler
Run Code

We are using another file line.kv, which enables us to build our widget tree in a declarative manner and helps to call back widget properties properly.

Filename: line.kv

Code 2 (Kivy)

# Ellipse
<LineEllipseA>:
 
    # Creating Canvas
    canvas:
        Color:
            rgba: 1, .1, .1, .9
        # Ellipse Creation
        Line:
            width: 2.
            ellipse: (self.x, self.y, self.width, self.height)
 
    # Label the figure
    Label:
        center: root.center
        text: 'Ellipse'
 
# Ellipse from 90 to 180
 
<LineEllipseB>:
    canvas:
        Color:
            rgba: 1, .1, .1, .9
        Line:
            width: 2.
            ellipse: (self.x, self.y, self.width, self.height, 90, 180)
    Label:
        center: root.center
        text: 'Ellipse from 90 to 180'

 
# Ellipse from 90 to 720, 10 segments
 
<LineEllipseC>:
    canvas:
        Color:
            rgba: 1, .1, .1, .9
        Line:
            width: 2.
            ellipse: (self.x, self.y, self.width, self.height, 90, 720, 10)
    Label:
        center: root.center
        text: 'Ellipse from 90 to 720, 10 segments'
        halign: 'center'
 
# Circle
<LineCircleA>:
    canvas:
        Color:
            rgba: .1, 1, .1, .9
        Line:
            width: 2.
            circle:
                (self.center_x, self.center_y, min(self.width, self.height)
                / 2)
    Label:
        center: root.center
        text: 'Circle'
 
 
# Circle from 90 to 180
<LineCircleB>:
    canvas:
        Color:
            rgba: .1, 1, .1, .9
        Line:
            width: 2.
            circle:
                (self.center_x, self.center_y, min(self.width, self.height)
                / 2, 90, 180)
    Label:
        center: root.center
        text: 'Circle from 90 to 180'
 
# Circle from 90 to 180, 10 segments
<LineCircleC>:
    canvas:
        Color:
            rgba: .1, 1, .1, .9
        Line:
            width: 2.
            circle:
                (self.center_x, self.center_y, min(self.width, self.height)
                / 2, 90, 180, 10)
    Label:
        center: root.center
        text: 'Circle from 90 to 180, 10 segments'
        halign: 'center'
 
 
# Circle from 0 to 360 
<LineCircleD>:
    canvas:
        Color:
            rgba: .1, 1, .1, .9
        Line:
            width: 2.
            circle:
                (self.center_x, self.center_y, min(self.width, self.height)
                / 2, 0, 360)
    Label:
        center: root.center
        text: 'Circle from 0 to 360'
        halign: 'center'
 

# Rectangle 
<LineRectangle>:
    canvas:
        Color:
            rgba: .1, .1, 1, .9
        Line:
            width: 2.
            rectangle: (self.x, self.y, self.width, self.height)
    Label:
        center: root.center
        text: 'Rectangle'
        
# Bezier 
<LineBezier>:
    canvas:
        Color:
            rgba: .1, .1, 1, .9
        Line:
            width: 2.
            bezier:
                (self.x, self.y, self.center_x - 40, self.y + 100,
                self.center_x + 40, self.y - 100, self.right, self.y)
    Label:
        center: root.center
        text: 'Bezier
You can also try this code with Online Python Compiler
Run Code

Output

Here is the output of our program.

Frequently Asked Questions

In Kivy, what does canvas do?

A Canvas is the root object of the Widget from which it draws anything.

In Kivy, what is a Builder?

The Builder is responsible for generating a Parser that parses a kv file and merges the results into the system's internal rules, templates, etc.

What do you mean by Kivy Designer?

Kivy Designer is a programme used by Kivy to create graphical user interfaces (GUIs) using Kivy Widgets. Widgets can be created, customised, and tested.

Conclusion

This article extensively discussed the Line Widget in the Kivy framework, its Implementation, and the steps to run the code using the Anaconda command prompt. We also discussed the Extended line widget. Check out the article on the 19 best Python frameworks to know more about different Python frameworks.

We hope this blog has helped you enhance your knowledge regarding Kivy. You can learn more about Python by visiting the Python category in Coding Ninjas Blogs. Visit our awesome articles to understand basic concepts of Python like Arrays, Lists, Iterators, generatorsVariablesFunctions, etc.

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, interview bundle, follow guided paths for placement preparations, and much more!!

We wish you Good Luck! Keep coding and keep reading Ninja!!

 

Live masterclass