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, generators, Variables, Functions, 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!!