Mini Paint Program with Python Turtle

Berivan Küçükkart
Yetkin Yayın
Published in
9 min readOct 16, 2022

Hello everyone, Hope you are all well. If you have browsed my previous articles, you must have understood that I have a special interest in the Python Turtle library.

When I met the Python Turtle library, the first thing that came to my mind was: Can I make a program similar to Paint using this library?

Paint user interface

Is it necessary and wise to do this? No. Is it fun? Hell yeah…

While constructing this idea, I had in my mind to place about 10–15 objects in the program with the help of functions, and then display the objects selected by the user on the screen.

I know it has nothing to do with the paint program in this state, but doing such a thing would be more effortless. I knew it would be as tiring as it was fun, and I didn’t want to spend a lot of time on this crazy idea when I was so busy. However, I did. If any of my university professors are reading this article, I’m sorry for my unwritten reports.

The Starry Night | Berry Van Gogh

I made the “The Starry Night” masterpiece you see above with my extremely useless program called “Drawing Adventure”.

The Starry Night | Vincent Van Gogh

Well… In my opinion, my masterpiece is right up there with Van Gogh’s “The Starry Night”. Alright, alright… I’m going to stop talking nonsense and move on to the coding part.

First of all, I must admit that it was not a professional work at all. But does the code work? Yes. No problem then.

Let me Introduce “Drawing Adventure” to You

In short, it is a screen where we can paint using triangle, square, star and circle shapes.

For example, you will draw a tree. You determine the shapes you will use for the tree, the colors of the shapes, and their positions. After giving all the information, it draws the shape you want for you, in the color you want, in the position you want. I told you it was cool but unnecessary.

Coding Process

As always, we start this time by importing the libraries we will use. I also import the Random library, but I guess I never used it. You can act like it doesn’t exist.

import libraries

Then of course we create our turtle and create our screen. We wrote the turt = turtle.Turtle() and wn = turtle.Screen() codes for these two operations. I named my turtle “turt” and my screen’s variable name is “wn” which is a commonly used name. These names are always up to you.

creating turtle and screen

The turt.shape(“blank”) code is a code used to define the turtle’s shape. There are many different forms of turtles. Some of them are “arrow”, “turtle”, “circle”, “square”, “triangle”, “classic”.

Click to go to the source

The code “wn.screensize()” is to determine the size of the screen. I determined it as 400x400px. This part is also up to you. Create a screen as large as you want. The “wn.title()” is to name the screen. The part you can observe in the image below had been created by this code.

wn.title(“write here your screen’s name”)

Now, we can move to the while loop part. In this program I used a while loop to keep the program running until there is an error. Inside the while loop there is an if loop and inside the if loop there is another if loop. Let me explain…

main part of the program

As I said, first we have to code a while loop. While everything is okay, do bla bla bla… Inside that loop I have a figure list and an input to collect data from the user. With that figure list, I aimed to limit the user to a few figures, thus making the program simpler. Let’s see what’s inside the if loop we saw above.

We have four different if loops representing each figure since the square and rectangular are same figures.

1. If input is Circle

Since the circle is the first element of the list(figure_list), we write

if input == figure_list[0]:

1.1. Color of Circle:

Then we will create a window that asks the user for the color of the shape.

color = turtle.textinput("Define the color", Type a color)

As you can see, in turtle module, the way to get input from the user is to use turtle.textinput(). It has two arguments: title and prompt.

In the image below, you can see what the title and prompt arguments represent on the screen.

turtle.textinput(title,prompt)

As can be seen in the picture, the title represents the name of the pop-up window, while the prompt represents the message you want to convey to the user.

1.2. Location of Circle:

Since the Turtle screen is a coordinate system and represents the exact midpoint (0,0) of the created screen, we will specify the location of each figure with the x and y axes.

Since our program will ask the user for the location of the figure that the user will draw, we will use the turtle.textinput() function again. Since there are two axes, we need to get data from the user for both the x and y axis.

However, since turtle.textinput() is intended to receive string values, we need to prefix it with a float.

x_axis = float(turtle.textinput("Define the x location, x = ?"))
y_axis = float(turtle.textinput("Define the y location, y= ?"))

1.3. Size of Circle:

The third input we will get from the user to draw a circle is the size of the figure. To do this, I created a variable called size_of_figure.

size_of_figure = int(turtle.textinput("Define the size", "Type the size"))

As you may understand, int is for converting string data to integers which is meaningful for a size.

1.4. All Codes of Circle:

Now that we have all the data we need to get from the user, we can use them to draw a circle.

if input == figure_list[0]:   color = turtle.textinput("Color", "Type a color:")
x_axis = float(turtle.textinput("Location", "x = ?"))
y_axis = float(turtle.textinput("Location", "y = ?"))
size_of_figure = int(turtle.textinput("Size", "Define the size"))
turt.penup() #turt was our turtle's name
turt.goto(x_axis,y_axis) #specific location we asked for
turt.pendown()
turt.color(color) #specific color we asked for
turt.begin_fill()
turt.circle(size_of_figure) #specific size we asked for
turt.end_fill

Congratulations, you have drawn your first figure. In the next process, we will draw a quadrilateral, a triangle and a star. Then, the coding process will be over. The time of art will begin…

We will adapt all the codes we used while drawing a circle to quadrilateral and triangle and use them again. Therefore, I will not explain every code in the next sections.

2. If input is Square or Rectangular

if input == figure_list[1] and figure_list[2]:

2.1. Color of Quadrilateral

color = turtle.textinput("Define the color", "Type a color")

2.2. Location of Quadrilateral

x_axis = float(turtle.textinput("Define the x location", "x = ?"))
y_axis = float(turtle.textinput("Define the y location", "y = ?"))

2.3. Size of Quadrilateral

Since a quadrilateral has two edges, a short one and a long one. We will define two variables for the size of it.

size_short= int(turtle.textinput("Define the size", "short edge"))
size_long= int(turtle.textinput("Define the size", "long edge"))

If the quadrilateral is a square both short and long edges will take the same value. So, no problem there.

2.4. All Codes of Quadrilateral

Quadrilaterals don’t have a special code like a circle, so we have to write specific codes to create a rectangle. As you can see below, I’ve instructed it to do the same moves using a for loop until a rectangle is formed.

if input == figure_list[1] and figure_list[2]:   color = turtle.textinput("Define the color", "Type a color")
x_axis = float(turtle.textinput("x location", "x = ?"))
y_axis = float(turtle.textinput("y location", "y = ?"))
size_short= int(turtle.textinput("size", "short edge"))
size_long= int(turtle.textinput("size", "long edge"))
turt.penup()
turt.goto(x_axis,y_axis)
turt.pendown()
turt.color(color)
turt.begin_fill()
for _ in range(2):
turt.forward(size_short)
turt.left(90)
turt.forward(size_long)
turt.left(90)
turt.end_fill()

3. If input is Triangle

if input == figure_list[3]:

3.1. Color of Triangle

color = turtle.textinput("Define the color", "Type a color")

3.2. Location of Triangle

x_axis = float(turtle.textinput("Define the x location", "x = ?"))
y_axis = float(turtle.textinput("Define the y location", "y = ?"))

3.3. Size of Triangle

size_of_tri = int(turtle.textinput("size","lenght of the edges: "))

3.4. All Codes ofTriangle

Like the quadrilateral, the triangle has no special code. We draw the triangle by repeating the same commands with the help of the for loop.

if input == figure_list[3]:   color = turtle.textinput("Define the color", "Type a color")
x_axis = float(turtle.textinput("x location", "x = ?"))
y_axis = float(turtle.textinput("y location", "y = ?"))
size_of_tri = int(turtle.textinput("size","lenght of edges"))
turt.penup()
turt.goto(x_axis,y_axis)
turt.pendown()
turt.color(color)
turt.begin_fill()
for _ in range(3):
turt.forward(size_of_tri)
turt.left(120)
turt.end_fill()

4. If input is Star

if input == figure_list[4]:

4.1. Color of Star

color = turtle.textinput("Define the color", "Type a color")

4.2. Location of Star

x_axis = float(turtle.textinput("Define the x location", "x = ?"))
y_axis = float(turtle.textinput("Define the y location", "y = ?"))

4.3. Size of Star

size_of_star = int(turtle.textinput("size","lenght of edges"))

4.4. All Codes of Star

The situation in the star is the same as in the quadrilateral and triangle. There is no specific code. We can create it with repeated commands in the for loop.

if input == figure_list[4]:   color = turtle.textinput("Define the color", "Type a color")
x_axis = float(turtle.textinput("x location", "x = ?"))
y_axis = float(turtle.textinput("y location", "y = ?"))
size_of_star = int(turtle.textinput("size","lenght of edges"))
turt.penup()
turt.goto(x_axis,y_axis)
turt.pendown()
turt.color(color)
turt.begin_fill()
for _ in range(5):
turt.forward(size_of_star)
turt.right(144)
turt.end_fill()

5. All Codes for the entire Program

import turtlefrom turtle import *turt = turtle.Turtle()turt.shape(“blank”)wn = turtle.Screen()wn.screensize(400,400)wn.title(“Drawing Adventure”)while True:   figure_list = [“circle”, “square” ,”rectangular”, “triangle”, “star” ]   input= turtle.textinput(“Type a Figure: “,”What do you want to draw?: “)   if input in figure_list:      if input == figure_list[0]:         color = turtle.textinput(“Color”, “Type a color:”)         x_axis = float(turtle.textinput(“Location”, “x = ?”))         y_axis = float(turtle.textinput(“Location”, “y = ?”))         size_of_figure = int(turtle.textinput(“Size”, “Define the size”))         turt.penup() #turt was our turtle’s name         turt.goto(x_axis,y_axis) #specific location we asked for         turt.pendown()         turt.color(color) #specific color we asked for         turt.begin_fill()         turt.circle(size_of_figure) #specific size we asked for         turt.end_fill      if input== figure_list[1] and figure_list[2]:         color = turtle.textinput(“Define the color”, “Type a color”)         x_axis = float(turtle.textinput(“x location”, “x = ?”))         y_axis = float(turtle.textinput(“y location”, “y = ?”))         size_short= int(turtle.textinput(“size”, “short edge”))         size_long= int(turtle.textinput(“size”, “long edge”))         turt.penup()         turt.goto(x_axis,y_axis)         turt.pendown()         turt.color(color)         turt.begin_fill()         for _ in range(2):            turt.forward(size_short)            turt.left(90)            turt.forward(size_long)            turt.left(90)         turt.end_fill()      if input == figure_list[3]:         color = turtle.textinput(“Define the color”, “Type a color”)         x_axis = float(turtle.textinput(“x location”, “x = ?”))         y_axis = float(turtle.textinput(“y location”, “y = ?”))         size_of_tri = int(turtle.textinput(“size”,”lenght of edges”))         turt.penup()         turt.goto(x_axis,y_axis)         turt.pendown()         turt.color(color)         turt.begin_fill()         for _ in range(3):            turt.forward(size_of_tri)            turt.left(120)         turt.end_fill()      if input == figure_list[4]:         color = turtle.textinput(“Define the color”, “Type a color”)         x_axis = float(turtle.textinput(“x location”, “x = ?”))         y_axis = float(turtle.textinput(“y location”, “y = ?”))         size_of_star = int(turtle.textinput(“size”,”lenght of edges”))         turt.penup()         turt.goto(x_axis,y_axis)         turt.pendown()         turt.color(color)         turt.begin_fill()         for _ in range(5):            turt.forward(size_of_star)            turt.right(144)         turt.end_fill()      wn.exitonclick()      break

Appendix: The Art Part

You may be thinking, what can be drawn using triangles and circles? Unleash your imagination. You can draw almost any figure you can see around you using circles, rectangles and triangles. The star is already our bonus figure.

Below I leave the picture I drew using my imagination. Although it took me two hours to make this drawing, I had fun doing it. As I mentioned at the beginning, drawing with this program is a complete waste of time. But it gives me the feeling of the beginning of something. Don’t you think so too?

Masterpiece no.2

See you in the next article, if you liked the article, follow for more.

--

--