
# Painting Tool

# imported necessary library
from tkinter import *
from tkinter.colorchooser import askcolor
from tkinter import messagebox
import tkinter.messagebox as mbox

# created a class for paint
class Paint(object):

    DEFAULT_PEN_SIZE = 5.0
    DEFAULT_COLOR = 'black'

    # init method defined
    def __init__(self):
        self.root = Tk()

        # button created for PEN
        self.pen_button = Button(self.root, text='PEN', command=self.use_pen,font=("Arial", 20), bg = "light green", fg = "blue", borderwidth=3, relief="raised")
        self.pen_button.grid(row=0, column=0)

        # button created for BRUSH
        self.brush_button = Button(self.root, text='BRUSH', command=self.use_brush,font=("Arial", 20), bg = "light green", fg = "blue", borderwidth=3, relief="raised")
        self.brush_button.grid(row=0, column=1)

        # button created for COLOR
        self.color_button = Button(self.root, text='COLOR', command=self.choose_color,font=("Arial", 20), bg = "light green", fg = "blue", borderwidth=3, relief="raised")
        self.color_button.grid(row=0, column=2)

        # button created for ERASER
        self.eraser_button = Button(self.root, text='ERASER', command=self.use_eraser,font=("Arial", 20), bg = "light green", fg = "blue", borderwidth=3, relief="raised")
        self.eraser_button.grid(row=0, column=3)

        # created a scale from 1 to 10
        self.choose_size_button = Scale(self.root, from_=1, to=10, orient=HORIZONTAL,width = 20,font=("Arial", 20), bg = "yellow", fg = "blue", borderwidth=3)
        self.choose_size_button.grid(row=0, column=4)

        # button created for EXIT
        self.exit_button = Button(self.root, text='EXIT', command=self.exit_win, font=("Arial", 20),bg="red", fg="blue", borderwidth=3, relief="raised")
        self.exit_button.grid(row=0, column=5)

        # created a drawing area
        self.c = Canvas(self.root, bg='white', width=1000, height=600)
        self.c.grid(row=1, columnspan=7)

        self.root.title("Painting Tool")
        self.setup()
        self.root.mainloop()

    # function for exiting
    def exit_win(self):
        if mbox.askokcancel("Exit", "Do you want to exit?"):
            self.root.destroy()

    # function to setup all things
    def setup(self):
        self.old_x = None
        self.old_y = None
        self.line_width = self.choose_size_button.get()
        self.color = self.DEFAULT_COLOR
        self.eraser_on = False
        self.active_button = self.pen_button
        self.c.bind('<B1-Motion>', self.paint)
        self.c.bind('<ButtonRelease-1>', self.reset)

    # function for pen
    def use_pen(self):
        self.activate_button(self.pen_button)

    # function for brush
    def use_brush(self):
        self.activate_button(self.brush_button)

    # function for choosing colour
    def choose_color(self):
        self.eraser_on = False
        self.color = askcolor(color=self.color)[1]

    # function for eraser
    def use_eraser(self):
        self.activate_button(self.eraser_button, eraser_mode=True)

    def activate_button(self, some_button, eraser_mode=False):
        self.active_button.config(relief=RAISED)
        some_button.config(relief=SUNKEN)
        self.active_button = some_button
        self.eraser_on = eraser_mode

    # function for painting
    def paint(self, event):
        self.line_width = self.choose_size_button.get()
        paint_color = 'white' if self.eraser_on else self.color
        if self.old_x and self.old_y:
            self.c.create_line(self.old_x, self.old_y, event.x, event.y,
                               width=self.line_width, fill=paint_color,
                               capstyle=ROUND, smooth=TRUE, splinesteps=36)
        self.old_x = event.x
        self.old_y = event.y

    def reset(self, event):
        self.old_x, self.old_y = None, None


# main funtion
if __name__ == '__main__':
    Paint()