Create a Simple Painting/Drawing App using Tkinter in Python: A Step-by-Step Tutorial

Create a Simple Painting/Drawing App using Tkinter in Python: A Step-by-Step Tutorial

Create a Simple Painting/Drawing App using Tkinter in Python: A Step-by-Step Tutorial
Create a Simple Painting/Drawing App using Tkinter in Python: A Step-by-Step Tutorial

Introduction

A painting/drawing app is a great way to explore the Tkinter library in Python. Tkinter is a built-in Python library for creating graphical user interfaces (GUIs). In this tutorial, we will be building a simple painting/drawing app using Tkinter. The app will have a main window that contains a canvas on which the user can draw. The user will also be able to select different colors and brush sizes for their drawing, as well as an eraser feature and a clear screen feature. Additionally, the user will be able to save their drawing as an image file.

Importing the necessary libraries

Before we can start building our app, we will need to import the Tkinter library and the filedialog library. The Tkinter library will be used to create the main window and the canvas, while the filedialog library will be used to allow the user to save their drawing as an image file.

from tkinter import *
from tkinter import filedialog

Creating the main window

Next, we will create the main window for our app. We will use the Tk() function to create the main window, and we will set the title of the window to “Painting App”.

root = Tk()
root.title("Painting App")

Adding the canvas

Now, we will add a canvas to the main window. The canvas will be the area on which the user can draw. We will use the Canvas() function to create the canvas and set its size to be 600 pixels by 400 pixels.

canvas = Canvas(root, width=600, height=400, bg="white")
canvas.pack()

Adding the color options

We will now add color options to the app. We will create a group of radio buttons that will allow the user to select different colors for their drawing.

color_var = StringVar()
color_var.set("black")
color_options = ["black", "red", "blue", "green", "yellow", "purple"]

for color in color_options:
    color_button = Radiobutton(root, text=color, variable=color_var, value=color)
    color_button.pack()
color_var = StringVar()
color_var.set("black")
color_options = ["black", "red", "blue", "green", "yellow", "purple"]

for color in color_options:
    color_button = Radiobutton(root, text=color, variable=color_var, value=color)
    color_button.pack()

Adding the brush size options

Next, we will add brush size options to the app. We will create a group of radio buttons that will allow the user to select different brush sizes for their drawing.

size_var = IntVar()
size_var.set(1)
size_options = [1, 2, 3, 4, 5]

for size in size_options:
    size_button = Radiobutton(root, text=size, variable=size_var, value=size)
    size_button.pack()

Adding the eraser feature

We will now add an eraser feature to the app. We will create a button that, when clicked, will change the color of the brush to white,allowing the user to erase their drawing.

def eraser():
    color_var.set("white")

eraser_button = Button(root, text="Eraser", command=eraser)
eraser_button.pack()
def eraser():
    color_var.set("white")

eraser_button = Button(root, text="Eraser", command=eraser)
eraser_button.pack()

Adding the clear screen feature

We will also add a clear screen feature to the app. We will create a button that, when clicked, will clear the entire canvas.

def clear_screen():
    canvas.delete("all")

clear_button = Button(root, text="Clear Screen", command=clear_screen)
clear_button.pack()

Adding the save feature

Finally, we will add a save feature to the app. We will create a button that, when clicked, will open a file dialog and allow the user to save their drawing as an image file.

def save_drawing():
    filepath = filedialog.asksaveasfilename(defaultextension=".png")
    if filepath:
        canvas.postscript(file=filepath, colormode='color')

save_button = Button(root, text="Save", command=save_drawing)
save_button.pack()

Complete Code

Here is the full code for the simple painting/drawing app that we have built in this tutorial:

from tkinter import *
from tkinter import filedialog

root = Tk()
root.title("Painting App")

canvas = Canvas(root, width=600, height=400, bg="white")
canvas.pack()

color_var = StringVar()
color_var.set("black")
color_options = ["black", "red", "blue", "green", "yellow", "purple"]

for color in color_options:
    color_button = Radiobutton(root, text=color, variable=color_var, value=color, command=lambda: canvas.config(bg=color_var.get()))
    color_button.pack()

size_var = IntVar()
size_var.set(1)
size_options = [1, 2, 3, 4, 5]

for size in size_options:
    size_button = Radiobutton(root, text=size, variable=size_var, value=size)
    size_button.pack()

def paint(event):
    x1, y1 = (event.x - size_var.get()), (event.y - size_var.get())
    x2, y2 = (event.x + size_var.get()), (event.y + size_var.get())
    canvas.create_oval(x1, y1, x2, y2, fill=color_var.get())

canvas.bind("<B1-Motion>", paint)

def eraser():
    color_var.set("white")

eraser_button = Button(root, text="Eraser", command=eraser)
eraser_button.pack()

def clear_screen():
    canvas.delete("all")

clear_button = Button(root, text="Clear Screen", command=clear_screen)
clear_button.pack()

def save_drawing():
    filepath = filedialog.asksaveasfilename(defaultextension=".png")
    if filepath:
        canvas.postscript(file=filepath, colormode='color')

save_button = Button(root, text="Save", command=save_drawing)
save_button.pack()

root.mainloop()

Running the app

We have now completed the code for our simple painting/drawing app. To run the app, we will call the mainloop() function on the Tkinter object.

root.mainloop()
Create a Simple Painting/Drawing App using Tkinter in Python: A Step-by-Step Tutorial

Conclusion

In this tutorial, we have built a simple painting/drawing app using the Tkinter library in Python. The app allows the user to draw on a canvas and has the ability to select different colors and brush sizes, an eraser feature, a clear screen feature, and the ability to save the drawing as an image file. This is just the basics and you can add more functionality and features to this app.

Print Friendly, PDF & Email
See also  New : DEMONSTRASI KONTEKSTUAL MODUL 2.2 PEMBELJARAN SOSIAL EMOSIONAL

Author

Leave a Reply