daawing app python

Solutions on MaxInterview for daawing app python by the best coders in the world

showing results for - "daawing app python"
Theo
22 Jan 2020
1from tkinter import *
2from tkinter.colorchooser import askcolor
3
4
5class Paint(object):
6
7    DEFAULT_PEN_SIZE = 5.0
8    DEFAULT_COLOR = 'black'
9
10    def __init__(self):
11
12
13
14
15        self.root = Tk()
16
17        self.pen_button = Button(self.root, text='pen', command=self.use_pen)
18        self.pen_button.grid(row=0, column=0)
19
20        self.brush_button = Button(self.root, text='brush', command=self.use_brush)
21        self.brush_button.grid(row=0, column=1)
22
23        self.color_button = Button(self.root, text='color', command=self.choose_color)
24        self.color_button.grid(row=0, column=2)
25
26        self.eraser_button = Button(self.root, text='eraser', command=self.use_eraser)
27        self.eraser_button.grid(row=0, column=3)
28
29        self.choose_size_button = Scale(self.root, from_=1, to=10, orient=HORIZONTAL)
30        self.choose_size_button.grid(row=0, column=4)
31
32        self.c = Canvas(self.root, bg='white', width=600, height=600)
33        self.c.grid(row=1, columnspan=5)
34
35        self.setup()
36        self.root.mainloop()
37
38    def setup(self):
39        self.old_x = None
40        self.old_y = None
41        self.line_width = self.choose_size_button.get()
42        self.color = self.DEFAULT_COLOR
43        self.eraser_on = False
44        self.active_button = self.pen_button
45        self.c.bind('<B1-Motion>', self.paint)
46        self.c.bind('<ButtonRelease-1>', self.reset)
47
48    def use_pen(self):
49        self.activate_button(self.pen_button)
50
51    def use_brush(self):
52        self.activate_button(self.brush_button)
53
54    def choose_color(self):
55        self.eraser_on = False
56        self.color = askcolor(color=self.color)[1]
57
58    def use_eraser(self):
59        self.activate_button(self.eraser_button, eraser_mode=True)
60
61    def activate_button(self, some_button, eraser_mode=False):
62        self.active_button.config(relief=RAISED)
63        some_button.config(relief=SUNKEN)
64        self.active_button = some_button
65        self.eraser_on = eraser_mode
66
67    def paint(self, event):
68        self.line_width = self.choose_size_button.get()
69        paint_color = 'white' if self.eraser_on else self.color
70        if self.old_x and self.old_y:
71            self.c.create_line(self.old_x, self.old_y, event.x, event.y,
72                               width=self.line_width, fill=paint_color,
73                               capstyle=ROUND, smooth=TRUE, splinesteps=36)
74        self.old_x = event.x
75        self.old_y = event.y
76
77    def reset(self, event):
78        self.old_x, self.old_y = None, None
79
80
81if __name__ == '__main__':
82    Paint()
83