Python | Creating a Simple Drawing App in kivy

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 Desktops applications.

Drawing App:

In this we are going to create a simple drawing App withe the help of kivy initially we are just making a canvas and a paint brush so that by moving the cursour 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 kivy.app import App  
      
# this restrict the kivy version i.e   
# below this kivy version you cannot   
# use the app or software   
kivy.require('1.9.0')  
  
# 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):
    pass
  
# 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()
        pb.center = touch.pos
        self.add_widget(pb)
          
    # On mouse movement how Paint_brush behave
    def on_touch_move(self, touch):
        pb = Paint_brush()
        pb.center = touch.pos
        self.add_widget(pb)
  
# Create the App class        
class DrawingApp(App):
    def build(self):
        return Drawing()
  
DrawingApp().run()

chevron_right


# .ky file:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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
    canvas:
        Color:
            rgb: rnd.random(), rnd.random(), rnd.random()
        Triangle:
            points:
                (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>:
    canvas:
        Color:
            rgb: .2, .5, .5
        Rectangle:
            size: root.size
            pos: root.pos

chevron_right


Output:



My Personal Notes arrow_drop_up

Competative Programmer

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.