Python
Python

 

 

Introduction

Drawing applications allow users to create illustrations interactively. This program demonstrates how to create a basic drawing app using Python’s Tkinter library, enabling users to draw freely on a canvas using the mouse.

Objective

The objective of this program is to create an interactive drawing application with basic functionality, including drawing lines on a canvas with the mouse.

Python Code

import tkinter as tk

class DrawingApp:
    def __init__(self, root):
        self.root = root
        self.root.title("Basic Drawing App")

        self.canvas = tk.Canvas(root, width=800, height=600, bg="white")
        self.canvas.pack()

        self.canvas.bind("", self.draw)
        self.last_x, self.last_y = None, None

    def draw(self, event):
        if self.last_x and self.last_y:
            self.canvas.create_line(self.last_x, self.last_y, event.x, event.y, fill="black", width=2)
        self.last_x, self.last_y = event.x, event.y

    def reset_position(self, event):
        self.last_x, self.last_y = None, None

if __name__ == "__main__":
    root = tk.Tk()
    app = DrawingApp(root)
    root.mainloop()

Program Explanation

This Python program uses the Tkinter library to create a basic drawing application. Here’s a breakdown of the program structure:

  • DrawingApp: The main application class that initializes the drawing canvas and binds mouse events.
  • draw: Handles the drawing logic by creating lines on the canvas based on mouse movement.
  • reset_position: Resets the last mouse position when the mouse button is released.

Steps to Run the Program

  1. Ensure Python is installed on your system.
  2. Copy the code into a file named drawing_app.py.
  3. Open a terminal or command prompt and navigate to the directory containing the file.
  4. Run the script using the command: python drawing_app.py.
  5. Use your mouse to draw on the canvas.
© 2024 Learn Programming. All Rights Reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)