Drawing Circles, Rectangles, and Lines in Kivy using Python: A Step-by-Step Guide

Posted by

How to Draw Circles, Rectangles and Lines in Kivy for Python

How to Draw Circles, Rectangles and Lines in Kivy for Python

Kivy is an open source Python library for developing multitouch applications. One of its features is the ability to draw shapes such as circles, rectangles and lines. In this article, we will explore how to use Kivy to draw these shapes in Python.

1. Drawing Circles

To draw a circle in Kivy, you can use the Ellipse widget. Here’s an example of how to do this:

        
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.scatter import Scatter
from kivy.uix.label import Label
from kivy.graphics import Color, Ellipse

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

        with self.canvas:
            Color(1, 0, 0)
            Ellipse(pos=(100, 100), size=(50, 50))

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

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

This code creates a red circle with a position of (100, 100) and a size of (50, 50).

2. Drawing Rectangles

Similarly, to draw a rectangle in Kivy, you can use the Rectangle widget. Here’s an example of how to do this:

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

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

        with self.canvas:
            Color(0, 1, 0)
            Rectangle(pos=(200, 200), size=(100, 50))

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

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

This code creates a green rectangle with a position of (200, 200) and a size of (100, 50).

3. Drawing Lines

To draw lines in Kivy, you can use the Line widget. Here’s an example of how to do this:

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

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

        with self.canvas:
            Color(0, 0, 1)
            Line(points=[300, 300, 400, 400])

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

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

This code creates a blue line with the starting point at (300, 300) and the ending point at (400, 400).

By using these simple examples, you can draw circles, rectangles and lines in Kivy for Python. This functionality can be useful for creating visual elements in your Kivy applications.

0 0 votes
Article Rating
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@positiveEnergyProgramming
6 months ago

Thanks for starting this tutorial. Another rabbit hole for me to go down and now its cross platform! Let's go!! Love this channel

@rethanon
6 months ago

Never used Kivy, good video to get started, thanks.