w3resource

Build a Basic Paint App in Python


Basic Paint App: Create a paint application with basic drawing tools and colors.

Input values:

User interacts with the paint application by selecting drawing tools, choosing colors, and drawing on the canvas.

Output value:

A visual representation of the canvas with the drawings created by the user using the tools and colors the user has chosen.

Example:

Input values:
1. Select a drawing tool: Pen
- User selects the pen tool for freehand drawing.
Output value:
Paint application switches to pen tool mode for drawing.
Input values:
2. Choose a color: Red
- User selects the color red for drawing.
Output value:
Selected color changes to red for drawing.
Input values:
3. Draw on the canvas
- User draws a freehand sketch on the canvas.
Output value:
Visual representation updates to display the freehand sketch drawn by the user in red color.
Input values:
4. Select the drawing tool: Rectangle
- User selects the rectangle tool for drawing shapes.
Output value:
Paint application switches to rectangle tool mode for drawing.
Input values:
5. Choose a color: Blue
- User selects blue for drawing rectangles.
Output value:
Selected color changes to blue for rectangles.
Input values:
6. Draw a rectangle on the canvas
- User draws a rectangle shape on the canvas.
Output value:
Visual representation updates to display the rectangle shape drawn by the user in blue color.
Input values:
7. Erase the drawing.
- User selects the eraser tool to erase part of the drawing.
Output value:
Visual representation updates to erase part of the drawing using the eraser tool.

Solution: Using tkinter for a Simple GUI Paint App

This solution uses tkinter, the standard GUI library in Python, to create a basic paint application. The app allows users to select different drawing tools, choose colors, and draw shapes on a canvas.

Code:

import tkinter as tk  # Import tkinter for GUI components
from tkinter import colorchooser  # Import colorchooser for color selection

class PaintApp:
    def __init__(self, root):
        """
        Initializes the Paint App with a canvas and drawing tools.
        Args:
            root: The main window of the application.
        """
        self.root = root
        self.root.title("Basic Paint App")

        # Create a canvas widget where the drawing will take place
        self.canvas = tk.Canvas(self.root, bg="white", width=600, height=400)
        self.canvas.pack()

        # Set initial color and tool
        self.color = "black"
        self.tool = "pen"

        # Create buttons for color, pen, rectangle, and eraser
        self.create_tools()

        # Bind mouse events to canvas methods
        self.canvas.bind("", self.paint)  # For drawing with left mouse button
        self.canvas.bind("", self.reset)  # Reset after drawing

    def create_tools(self):
        """Creates buttons for choosing colors, drawing tools, and clearing the canvas."""
        tools_frame = tk.Frame(self.root)
        tools_frame.pack()

        # Button for selecting pen tool
        pen_button = tk.Button(tools_frame, text="Pen", command=lambda: self.select_tool("pen"))
        pen_button.pack(side="left")

        # Button for selecting rectangle tool
        rect_button = tk.Button(tools_frame, text="Rectangle", command=lambda: self.select_tool("rectangle"))
        rect_button.pack(side="left")

        # Button for selecting eraser
        erase_button = tk.Button(tools_frame, text="Eraser", command=lambda: self.select_tool("eraser"))
        erase_button.pack(side="left")

        # Button for color picker
        color_button = tk.Button(tools_frame, text="Color", command=self.choose_color)
        color_button.pack(side="left")

    def select_tool(self, tool):
        """Sets the current drawing tool."""
        self.tool = tool

    def choose_color(self):
        """Opens a color chooser dialog and sets the selected color."""
        self.color = colorchooser.askcolor()[1]

    def paint(self, event):
        """Draws on the canvas based on the selected tool and color."""
        if self.tool == "pen":
            # Draws a line for the pen tool with the selected color
            self.canvas.create_line(event.x, event.y, event.x + 1, event.y + 1, fill=self.color)
        elif self.tool == "rectangle":
            # Draws a rectangle outline with the selected color
            self.canvas.create_rectangle(event.x, event.y, event.x + 50, event.y + 30, outline=self.color)
        elif self.tool == "eraser":
             # Draws with "white" color to simulate erasing by drawing over existing elements
            self.canvas.create_line(event.x, event.y, event.x + 10, event.y + 10, fill="white", width=10)


    def reset(self, event):
        """Resets the drawing process."""
        pass  # Can add functionality here if needed

# Initialize the application
root = tk.Tk()
app = PaintApp(root)
root.mainloop()

Output:

Python: Simple Paint Application.

Explanation:

  • Initializes a tkinter window with a canvas for drawing.
  • Provides buttons to switch between drawing tools and color selection.
  • Uses mouse events to handle drawing operations on the canvas.


Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/projects/python/python-project-simple-paint-application.php