Sai A Sai A
Updated date Feb 15, 2024
In this blog, we will learn to create a simple paint program in Python from scratch. Explore methods to add colors, adjust brush sizes, and implement undo functionality.

Introduction:

In the digital world, expressing creativity through programming is not only possible but also incredibly fun and rewarding. In this blog post, we will embark on a journey to create a simple paint program using Python

Method 1: Using Tkinter Library

Our first method involves using the Tkinter library, a standard GUI (Graphical User Interface) toolkit for Python. Tkinter provides easy-to-use functions for creating windows, buttons, and other GUI elements.

import tkinter as tk

def paint(event):
    x1, y1 = (event.x - 1), (event.y - 1)
    x2, y2 = (event.x + 1), (event.y + 1)
    canvas.create_oval(x1, y1, x2, y2, fill="black")

root = tk.Tk()
root.title("Simple Paint Program")

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

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

root.mainloop()

This code creates a simple paint program using Tkinter. We start by importing the Tkinter library and defining a function paint(event) that draws an oval (dot) on the canvas at the location of the mouse pointer whenever the left mouse button is pressed and moved (<B1-Motion> event). We then create a Tkinter window (root), a canvas widget (canvas), and bind the paint function to the canvas. Finally, we start the main event loop with root.mainloop().

Output:

Upon running the program, a window titled "Simple Paint Program" will appear with a white canvas. Clicking and dragging the mouse on the canvas will draw black dots, allowing you to create simple drawings and paintings.

Method 2: Adding Colors and Brush Sizes

Let's enhance our paint program by adding the ability to choose different colors and brush sizes. We will modify our existing code to include options for color selection and brush size adjustment.

import tkinter as tk
from tkinter import colorchooser

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

def change_color():
    color = colorchooser.askcolor(title="Select Color")
    if color:
        current_color.set(color[1])

def change_brush_size(size):
    global brush_size
    brush_size = size

root = tk.Tk()
root.title("Simple Paint Program")

current_color = tk.StringVar(root, "black")
brush_size = 2

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

color_button = tk.Button(root, text="Choose Color", command=change_color)
color_button.pack()

size_slider = tk.Scale(root, from_=1, to=10, orient=tk.HORIZONTAL, label="Brush Size", command=change_brush_size)
size_slider.pack()

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

root.mainloop()

In this modified version of our paint program, we've added a color selection button (color_button) and a brush size slider (size_slider). The change_color() function opens a color chooser dialog using colorchooser.askcolor() and sets the selected color as the current color. The change_brush_size(size) function updates the brush_size global variable based on the selected size from the slider.

Output:

Now, in addition to drawing black dots, you can choose different colors using the "Choose Color" button and adjust the brush size using the slider, enabling more customization and creativity in your artwork.

Method 3: Implementing Undo Functionality

As artists, we often make mistakes and want to undo our actions. Let's implement an undo functionality in our paint program to erase the last stroke drawn on the canvas.

import tkinter as tk
from tkinter import colorchooser

def paint(event):
    x1, y1 = (event.x - brush_size), (event.y - brush_size)
    x2, y2 = (event.x + brush_size), (event.y + brush_size)
    canvas.create_oval(x1, y1, x2, y2, fill=current_color.get())
    draw_history.append(canvas.create_oval(x1, y1, x2, y2, fill=current_color.get()))

def change_color():
    color = colorchooser.askcolor(title="Select Color")
    if color:
        current_color.set(color[1])

def change_brush_size(size):
    global brush_size
    brush_size = size

def undo():
    if draw_history:
        canvas.delete(draw_history.pop())

root = tk.Tk()
root.title("Simple Paint Program")

current_color = tk.StringVar(root, "black")
brush_size = 2
draw_history = []

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

color_button = tk.Button(root, text="Choose Color", command=change_color)
color_button.pack()

size_slider = tk.Scale(root, from_=1, to=10, orient=tk.HORIZONTAL, label="Brush Size", command=change_brush_size)
size_slider.pack()

undo_button = tk.Button(root, text="Undo", command=undo)
undo_button.pack()

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

root.mainloop()

In this iteration, we've added an "Undo" button (undo_button) that triggers the undo() function when clicked. The undo() function removes the last drawn stroke from the canvas by deleting the corresponding object from the draw_history list, which keeps track of all drawn objects.

Output:

With the addition of the "Undo" button, you can now easily correct mistakes by removing the last stroke drawn on the canvas, providing more control and flexibility in your artistic process.

Conclusion:

We have successfully created a simple paint program in Python, exploring different methods to enhance its functionality along the way. From basic drawing capabilities to color selection, brush size adjustment, and even undo functionality.

Comments (0)

There are no comments. Be the first to comment!!!