Python Paint Program:

Meta-Description:

If you’re an artist who loves to print or sketch, then you’ll love being able to code your own paint app! Follow along with this article to find out how to create a painting program using Python and the Turtle library.

Introduction: 

In this article, we are going to be using Python and the Tkinter library to create a simple paint program that will allow you to create artistic masterpieces. The program will include a pen, a color selector, a menu to choose a pen size, and an eraser.

Python Paint game

This project is a great way to learn more about making functional programs by creating a useful program that can be used for a wide variety of purposes. It’s a great way to build both your creative thinking and problem-solving skills while making a functional computer program.

Who is this Project For?

This is an intermediate project for those who are new to Python. Before starting this project, you should already have experience with classes, using the __init__() method, and creating simple Tkinter GUIs (Graphical User Interfaces) as these are the concepts that you will use and build upon as you code this project.

What Will We Learn?

This program focuses on creating a GUI using the Tkinter library, which is used to display the print program. Classes and methods are also used to set up the ‘self’ object and to create the program’s functions. These concepts are more advanced but are important to understand, making this the perfect program to improve these essential skills.

Features to Consider:


Pseudo Code:

There are many ways that this game could be programmed, but below is the pseudocode that was used to make this paint program:

Import Tkinter and colorchooser libraries

Create an object called paint, with the color black and a size of 10 pixels

Create the Tkinter window

In row 0, column 0, create a button called “pen” that runs the function usePen

In row 0, column 2, create a button called “color” that runs the function chooseColor

In row 0, column 3, create a button called “eraser” that runs the function useEraser


Orient the button horizontally

Create a “choose size” button in row 0, column 4

Create a 600x600 canvas with a white background

Set the canvas to row 1 and ake it span 5 columns

Run the setup function

Run the mainloop function


Function setup(self):

Create variables for old x and y-coordinates and set them to 0

Set the default line width to 0

Set the color to the default color

Set the eraser state to off

Set the pen button to active


Function usePen(self):

         Use the pen if the pen button is active


Function chooseColor(self):

         Set the eraser to false

Set the color to the user’s chosen color


  Function useEraser(self):

         Set eraser to true


    def buttonPressed(self):

        If the button is pressed style it to look sunken in

If the button is not pressed return it to its’ original state


Function paint(self, event):

Set the pen size to the user’s selected size

If the eraser is on, tet pen color to white, otherwise, set it to the selected color

Draw a line from the old x and y-coordinates to the pen’s current location

Set the old x and y-coordinates equal to the current x and y-coordinates


Run the paint function

Python Paint Program

Python Paint Program

Main Steps:

This project can be broken down into 4 main steps: 

  1. Import libraries and set up the Paint class
  2. Create the Self and Setup functions
  3. Create the Pen, Erase, Colour, and Size functions
  4. Create the Paint function

Step 1: Import Libraries and Set Up The Paint Class

The first thing we’ll need to do when creating this game is import all of the required libraries. For this program, we will be using the Tkinter library to create the GUI as well as the askcolor module to allow users to select multiple colors for their pen. Importing these libraries is necessary because the program would not be able to run without them. We can import these libraries using this code:

from tkinter import *from tkinter.colorchooser import askcolor

Next, we need to set up the Paint class. This is a class that will store objects used in the program, and it is what will be called in order to make the program function. This will be done using the general class definition syntax, as seen below:

class Paint(object):
    DEFAULT_PEN_SIZE = 5.0
    DEFAULT_COLOR = 'black'
Hint: You will also notice that as a part of the class we have included two variables. These will be used to set default pen colors and sizes for the program, so feel free to change them to whatever you’d like.

Step 2: Create the Self and Setup Functions

Now it’s time to create Self and Setup functions for the program. The self function will simply set up the main GUI window, which includes buttons, the canvas, and the menu to choose a pen size. The setup function will set up the pen by setting its coordinates, color, size, etc. However, we will start with the self function so that it can be used later in the setup function. For the self function, we will type:

def __init__(self):
        self.root = Tk()
        self.penButton = Button(self.root, text='pen', command=self.usePen)
        self.penButton.grid(row=0, column=0)

        self.colorButton = Button(self.root, text='color', command=self.chooseColor)
        self.colorButton.grid(row=0, column=2)

        self.eraserButton = Button(self.root, text='eraser', command=self.useEraser)
        self.eraserButton.grid(row=0, column=3)

        self.choose_sizeButton = Scale(self.root, from_=1, to=10, orient=HORIZONTAL)
        self.choose_sizeButton.grid(row=0, column=4)

        self.c = Canvas(self.root, bg='white', width=600, height=600)
        self.c.grid(row=1, columnspan=5)

        self.setup()
        self.root.mainloop()

Note that the majority of this function consists of creating buttons and aligning them in the GUI. Each of these buttons follows the same format as well - the command takes input for the button text, function, and alignment using columns and rows. The general format is as follows:

self.buttonName = Button(self.root, text='buttonText', command=self.buttonCommand)
        self.eraserButton.grid(row=0, column=0)

Now that the self function is complete, it’s time to create the setup function. This function’s code should look like this:

def setup(self):
        self.old_x = None
        self.old_y = None
        self.line_width = self.choose_sizeButton.get()
        self.color = self.DEFAULT_COLOR
        self.eraser_on = False
        self.activeButton = self.penButton
        self.c.bind('<B1-Motion>', self.paint)

The first couple lines of code ensure that no pen coordinates are already stored in the program. Then, we will add a line of code to check what pen size and color the player has selected and these are applied to the pen. Finally, we will set the eraser to false and then check the status of the pen button to make sure that it has been selected. Once this is done, it’s time to move on to the program’s other functions

Python Paint Program

Step 3: Create the Pen, Erase, Colour, and Size Functions

Now that we have finished creating the self and setup functions for this program, it’s time to secret the rest of the functions. We will start by creating the pen and color functions. These functions are both fairly short and simple, as they both simply take input from the button or selection menu that they are associated with. The code for this section will look like this:

def usePen(self):
        self.buttonPressed(self.penButton)

def chooseColor(self):
        self.eraser_on = False
        self.color = askcolor(color=self.color)[1]
Python Paint Program

Python Paint Program

As you can see, the usePen function simply checks the state of the pen button, while the chooseColour function sets the eraser to false and sets the pen to the color that the user has selected. Next, it’s time to create the useEraser and buttonPressed functions. The eraser function is almost the same as the usePen function, except it sets the state of the eraser boolean to true. The buttonPressed function, however, is a little different. It involves formatting the button to change looks when it is actively being pressed, as well as changing the setting of the eraser-mode variable to turn the eraser on. Once this is complete, the code should be:

def useEraser(self):
        self.buttonPressed(self.eraserButton, eraser_mode=True)

    def buttonPressed(self, someButton, eraser_mode=False):
        self.activeButton.config(relief=RAISED)
        someButton.config(relief=SUNKEN)
        self.activeButton = someButton
        self.eraser_on = eraser_mode

Step 4: Create the Paint Function 

Finally, we’re on to the last step, which will include creating and calling the paint function so that the game will run.

def paint(self, event):
        self.line_width = self.choose_sizeButton.get()
        paint_color = 'white' if self.eraser_on else self.color
        if self.old_x and self.old_y:
            self.c.create_line(self.old_x, self.old_y, event.x, event.y, width=self.line_width, fill=paint_color, capstyle=ROUND, smooth=TRUE, splinesteps=36)
        self.old_x = event.x
        self.old_y = event.y

if __name__ == '__main__':
    Paint()

Note that each of these labels follows the same format: the text that will be written, the font and font size, then the color of the text. This format will be used for each label, meaning that once you make one it is simple to make the others using the same format. Finally, we will create an input box where players can type their response, and a start button to begin the game, and then the game is done! 

Once all of this is complete, the code will look like this:

 def paint(self, event):
        self.line_width = self.choose_sizeButton.get()
        paint_color = 'white' if self.eraser_on else self.color
        if self.old_x and self.old_y:
            self.c.create_line(self.old_x, self.old_y, event.x, event.y, width=self.line_width, fill=paint_color, capstyle=ROUND, smooth=TRUE, splinesteps=36)
        self.old_x = event.x
        self.old_y = event.y

if __name__ == '__main__':
    Paint()

As you can see from the code above, the first step is to set the line width to whatever the selected size is. Next, the paint color is set to white when the eraser is on. Next, there will be a line drawn from the old point to the new point in the canvas, and the mouse’s location is followed to ensure that the pen draws on the canvas. Once this section of code is finished, the project is done!

Project Complete!

Python Paint Program

Now the project is complete! We hope you’ve had fun creating this paint program using Python and the Tkinter library, and hopefully you’ve learned more about programming with Python! Feel free to test your code now and see how it works. If you’re stuck or have any issues with your code, try reviewing it again either in your text editor or by looking at the code included in the article as a reference. 

With the advent of the computing era and demand for software developers, kids should be able to have a grip on basic knowledge regarding coding and if they ever decide to make it their career in the future. In this age of modern technology, kids have quickly become tech-savvy with access to computers, games, and laptops from a very young age, and developing coding skills won't be that difficult if they started young.

Beginning coding and learning the programming principles can be done by children as early as seven years old. Coding for kids has become increasingly popular in recent years as technology gets more incorporated into our daily lives. Learning to code as a child can set the path for a lifetime of opportunities.

What Is Coding?

Coding is the process of creating or programming a computer through an imaginative procedure that directs the machine to perform a variety of tasks. It is accomplished by using codes or instructions that are required to function properly as essential to a computer. Codes are simply a collection of digital inputs, actions implemented on those inputs, outputs, and their resulting reactions.

Coding abilities are incredibly important in today's digital era for employees in almost every industry. In addition, several studies have shown that youngsters who learn to code from the commencement of their education do better during their lives. It is predicted by the bureau of labour statistics that we’ll see an increase in information and technology professions at the rate of 13 percent from 2020-2030.

When Is The Optimal Time To Start Coding For A Child?

Each child is a one-of-a-kind individual. Children can implement a variety of things at varied rates and in various ways. Experts believe that youngsters should learn to code at a young stage of development. As stated by MIT, children should begin studying coding languages like Scratch Jr while they are between the ages of 5 and 7. People frequently do this by playing various rational games that simulate the dexterity of coding.

At What Age Can A Child Start Coding?

Does It Become Easier To Code If One Starts Very Young?

Experts generally believe that youngsters should be taught to code as early as possible. Every child develops at their speed and in distinct ways. Even yet, youngsters as young as 5 or 6 can begin learning basic exercises. Introduce several logical games that replicate the talent of coding to accomplish this. You can go to more challenging exercises after a year or so.

Several studies have found that children who begin coding in elementary school earn higher academic results throughout their education.

What Necessary Benefits Can A Child Reap From Coding Skills?

In the digital era, coding is measured as a vital skill. As a result, understanding and using technology is a requirement in this day and age. It pertains to a lot of advantages when it comes to cultivating the skills of coding. Children should learn to code for the following reasons:

What Is The Best Way For Kids To Begin Learning To Code?

At What Age Can A Child Start Coding?

To begin with, coding, doing too much too soon can lead to frustration. If your child is going to benefit from coding, you want them to like it. Hence, it's better to take things gently at first, to take your time, and to set minor goals along the road.

Here's how to familiarize coding to your children in a way that they will like and thrive at:

  1. Start With The Fundamental Basics: It is critical to understand the fundamentals to master coding. With some viewing or reading, you may introduce coding to your children. All of the coding principles are covered in several books and articles. If your child prefers to learn by watching videos, YouTube has a wealth of resources. Once the youngsters grasp the basics of coding, they'll be able to describe it in a few phrases to their friends and relatives. If you're not familiar with coding, you might learn something new as well. Check out Code Monkey; this website offers programming language for kids; they can build on their skills and start from scratch with little to no experience at all.
  2. Try Something Small: Your children are enthralled and eager for more information after learning that foundation of various robots, video games, and films start with coding. If you give them a challenging task, they will quickly become frustrated. A complex game should not be their first endeavor. Begin by giving them a tiny gift. This tends to be the most effective technique to keep students interested and involved before they dive headfirst into the realm of programming.
  3. Move Onto Bigger Tasks: They can progress to better things if they have mastered the fundamentals. The following step should not be to aim for the moon. Just fixate on things they're used to. Take small steps and make sure you comprehend everything. It aids kids in understanding how numerous elements come together to bring something to life. Kids will be preparing to develop simple apps as a result of these processes, which is the foundation for creating something bigger. This will allow kids to observe how various bits and pieces work together to create something new. Maybe they'll make a simple mobile app or an internet game.
  4. Learn More: Practice makes a person perfect. Don't give up easily if things don't go according to the child’s plan or they aren't getting the grasp of coding. For instance, debugging can be tiring to solve, but it can feel very rewarding once a kid learns how to take care of it.

Discussing something new with others is a terrific method to help it keep in your mind. Encourage your children to discuss the topic with their peers. For instance, they may be able to connect it to the most recent video game they're playing; your child may be able to explain how the game would not exist without coding.

Better yet, introduce your kid to scratch, a website, and a community or programmer kids to code and share with each other their outstanding projects. This will encourage them to interact with others and take constructive criticism with no fuss at all.

They can inquire how coding aids in powering robots with their science teacher at school. It's possible that no one in your child's everyday circle is interested in talking about coding. You'll need to broaden your horizons in this case. Visit a technology museum or enroll your child in a coding-related after-school program.

At What Age Can A Child Start Coding?

The Takeaway!

Early elementary school is the optimum time for children to begin coding. Strengthening cognitive skills through coding helps to plant seeds for eventual complex knowledge. We have confidence in the fact that the optimal age for children to begin learning to code is around the age of 6-7.

Suppose children are taught to think rationally and develop a problem-solving mindset early in life. In that case, they will comprehend complicated coding ideas later in life. Even if students decide to abandon coding later in life, having a fundamental understanding of technology principles will be helpful in the future.

coding class for kids


Code your future!

Coding and Math envision a brighter future

Geekedu was founded in 2015 by a group of like-minded geeks working at Google and Amazon. We wanted to find a way to share our passion for Coding and Mathematics and inspire kids to become future leaders, influencers and inventors. Our “Geek philosophy” is that learning to code should be accessible to every child and teenager. We are partnered with over 120 schools, academies, and organizations in North America to encourage kids to achieve and innovate.
Our Instructor