Table of contents
1.
Introduction
2.
Drawing App
3.
Implementation
3.1.
Basic Approach
3.1.1.
Python Code
3.1.2.
.kv Code
3.1.3.
Output
3.1.4.
Terminal Output
4.
Frequently Asked Questions
4.1.
Is Kivy free for commercial use?
4.2.
Which Python GUI is most straightforward?
4.3.
Is KIVY better than Tkinter?
4.4.
Can I use pygame in KIVY?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Drawing App in Kivy

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

Introduction

Application development, Desktop, and Android are a significant part of python programming. That is where Kivy can be a handy tool for python developers. Kivy is a platform-independent GUI tool that you can run on Android, IOS, Linux, Windows, or any other operating system. Although we majorly utilize Kivy for Android development, its functionality is not limited. 

In this article, we will walk through the implementation of the Drawing App in Kivy python using a .kv file with the help of a few examples. This article assumes that you have worked with Python programming. If you are not sure of your knowledge of Python, you can check out our Basics of Python article and come back here.

Drawing App

The simple Kivy Drawing App API lets you click to draw using a canvas and a paintbrush, creating a Drawing App. Here we will create a simple drawing app with the help of kivy. Initially, we will create a canvas and a paintbrush so that you can draw by moving the cursor, similar to a drawing Application.

We will use a dynamic approach to add widgets. You need to add widgets in the Python file, For adding widgets dynamically, at run time, as per user interaction. 

Implementation

In our implementation, we will use widgets, random, and layout to make our App more presentable and improve the Graphic User Interface(GUI).

Basic Approach

You can follow the basic approach laid out below to create your Drawing App, similar to the example given in this article.

  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 a .kv file(same name as your app class; you can also name Drawing .kv for DrawingApp class)
  10. Return the widget, layout, etc., class
  11. Run an instance of the class
     

We will look at a sample python application code using Kivy to demonstrate the working of the approach mentioned above for the Drawing App.

Python Code

# drawing.py
   
# import the kivy module    
import kivy
   
from kivy.app import App
   
kivy.require('1.9.0')

# Widgets are elements of GUI
# that form part of the User Experience.
from kivy.uix.widget import Widget

# Relative layout allows you to set relative coordinates for children elements.
from kivy.uix.relativelayout import RelativeLayout

# the Widget class
class Paint_brush(Widget):
    pass

# the layout class
# where you define the working of
# the Paint_brush() class
class Drawing(RelativeLayout):

    # Paint_brush behavior on mouse press
    def on_touch_down(self, touch):
        pb = Paint_brush()
        pb.center = touch.pos
        self.add_widget(pb)
       
    # Paint_brush behaviour on mouse movement
    def on_touch_move(self, touch):
        pb = Paint_brush()
        pb.center = touch.pos
        self.add_widget(pb)

# the App class
class DrawingApp(App):
    def build(self):
        return Drawing()

DrawingApp().run()
You can also try this code with Online Python Compiler
Run Code

.kv Code

# Drawing.kv


#code for Paint Brush
<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)


# creating Drawing Pad        
<Drawing>:
    canvas:
        Color:
            rgb: .2, .5, .5
        Rectangle:
            size: root.size
            pos: root.pos

Output

Terminal Output

Frequently Asked Questions

Is Kivy free for commercial use?

Kivy is entirely free of cost. It comes under an MIT license (from 1.7. 2) and LGPL 3 for previous versions. You can use Kivy in a commercial product. The toolkit is professionally developed, used, and backed.

 

Which Python GUI is most straightforward?

Tkinter is one of the most in-demand GUI libraries available on Python. Its easy-to-learn and straightforward syntax make it one of the first recommended choices for beginners to GUI development.

 

Is KIVY better than Tkinter?

Due to its complexity in learning and installation for beginner to GUI development compared to Tkinter, Kivy is often not preferred over Tkinter.\

 

Can I use pygame in KIVY?

Kivy and pygame are both incompatible with each other. So as a user, you cannot draw anything on pygame using Kivy.

Conclusion

In this article, we have extensively discussed the Drawing App in Kivy and its implementation in Python with the help of examples. There are many more Python frameworks and tools that you can learn and utilize for application development.

We hope that this blog has helped you enhance your knowledge of Kivy Python and if you would like to learn more, check out our articles on Basics of Python and Popular Python LibrariesDo upvote our blog to help other ninjas grow, and head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.

 

Happy Learning!

Live masterclass