Open In App

Python | Creating a Simple Drawing App in kivy

Last Updated : 29 Nov, 2022
Like Article

Kivy is a platform-independent GUI tool in Python. As it can be run on Android, IOS, Linux and Windows, etc. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktop applications.

Kivy Tutorial – Learn Kivy with Examples.

Drawing App:

In this we are going to create a simple drawing App with the help of kivy initially we are just making a canvas and a paintbrush so that by moving the cursor you can just feel like a drawing Application.
In this, the widgets are added dynamically. If widgets are to be added dynamically, at run-time, depending on user interaction, they can only be added in the Python file.

We are using widgets, layout, random to make it good.  

Now Basic Approach of the App:

1) import kivy
2) import kivy App
3) import Relativelayout
4) import widget
5) set minimum version(optional)
6) Create widget class as needed
7) Create Layout class
8) create the App class
9) create .kv file
10) return the widget/layout etc class
11) Run an instance of the class

Implementation of the Code: 

# .py file: 


# Program to explain how to create drawing App in kivy 
# import kivy module    
import kivy  
# base Class of your App inherits from the App class.    
# app:always refers to the instance of your application   
from import App 
# this restrict the kivy version i.e  
# below this kivy version you cannot  
# use the app or software  
# Widgets are elements of a
# graphical user interface that
# form part of the User Experience.
from kivy.uix.widget import Widget
# This layout allows you to set relative coordinates for children.
from kivy.uix.relativelayout import RelativeLayout
# Create the Widget class
class Paint_brush(Widget):
# Create the layout class
# where you are defining the working of
# Paint_brush() class
class Drawing(RelativeLayout):
    # On mouse press how Paint_brush behave
    def on_touch_down(self, touch):
        pb = Paint_brush() = touch.pos
    # On mouse movement how Paint_brush behave
    def on_touch_move(self, touch):
        pb = Paint_brush() = touch.pos
# Create the App class       
class DrawingApp(App):
    def build(self):
        return Drawing()

Code Explanation:

  1. The code starts with importing the kivy module.
  2. The code then defines a base class of your App inheriting from the App class.
  3. This restricts the kivy version i.e below this kivy version you cannot use the app or software.
  4. The next line is where we create Paint_brush() class which is a Widget and it’s layout, Drawing().
  5. In order to define how paint brush behaves when touched on screen, we have defined two methods in its layout: on_touch_down() and on_touch_move().
  6. These are called when user touches down or moves their mouse over it respectively.
  7. In order to create an instance of your application, you need to call build() method in your App class which returns a widget object that can be added as child element in RelativeLayout (Drawing).
  8. The code is a simple example of how to create a drawing app in kivy.
  9. First, we import the kivy module.
  10. Next, we define our base class as App and then we have defined the Widget class which inherits from the App class.
  11. We create a layout class where you are defining the working of Paint_brush() class and then finally we create our application which is DrawingApp().

# .ky file: 


# Drawing.kv implementation
# for assigning random color to the brush
#:import rnd random
# Paint brush coding
<Paint_brush >:
    size_hint: None, None
    size: 25, 50
            rgb: rnd.random(), rnd.random(), rnd.random()
                (self.x, self.y, self.x + self.width / 4, self.y,
                 self.x + self.width / 4, self.y + self.height / 4)
# Drawing pad creation
<Drawing >:
            rgb: .2, .5, .5
            size: root.size
            pos: root.pos

Code Explanation:

  1. The code starts with the import statement.
  2. This imports the random module into our program.
  3. The next line is a Paint_brush class definition that has size_hint and size properties, but no canvas property.
  4. The next line creates an empty 25×50 rectangle on the screen by using root as its parent object (the window).
  5. It then assigns it a random color from rnd.random(), rnd.random(), and rnd.random().
  6. The last two lines create a triangle shape at self’s x coordinate, y coordinate, x + width/4, y + height/4 coordinates on the screen in green color using self as its parent object (the window).
  7. The code attempts to assign a random color to the brush when drawing.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads