Python Project – Paint

Python Project – Paint

from tkinter import *
from tkinter.colorchooser import askcolor

# create a Tkinter window
window = Tk()
window.title(“Paint Program”)

# create a canvas to draw on
canvas = Canvas(window, width=500, height=500, bg=”white”)

# define functions for drawing
def start(event):
    global prev_x, prev_y
    prev_x, prev_y = event.x, event.y

def draw(event):
    global prev_x, prev_y
    canvas.create_line(prev_x, prev_y, event.x, event.y, width=5, fill=color)
    prev_x, prev_y = event.x, event.y

def choose_color():
    global color
    color = askcolor()[1]

def clear_canvas():

# create buttons for choosing color and clearing the canvas
Button(window, text=”Choose Color”, command=choose_color).pack()
Button(window, text=”Clear Canvas”, command=clear_canvas).pack()

# bind mouse events to drawing functions
canvas.bind(“<Button-1>”, start)
canvas.bind(“<B1-Motion>”, draw)

# start the main event loop

Leave a Reply

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