1+ from tkinter import *
2+ from tkinter .colorchooser import askcolor
3+ from PIL import ImageTk , Image
4+
5+ class Paint (object ):
6+
7+ DEFAULT_PEN_SIZE = 5.0
8+ DEFAULT_COLOR = 'black'
9+
10+ def __init__ (self ):
11+ self .root = Tk ()
12+ self .root .title ('Paint' )
13+ self .root .geometry ('500x300' )
14+ self .root .maxsize (500 ,300 )
15+ self .root .minsize (500 ,300 )
16+
17+
18+ self .paint_tools = Frame (self .root ,width = 100 ,height = 300 ,relief = RIDGE ,borderwidth = 2 )
19+ self .paint_tools .place (x = 0 ,y = 0 )
20+
21+ self .pen_logo = ImageTk .PhotoImage (Image .open ('pen.png' ))
22+ self .p = Label (self .paint_tools , text = "pen" ,borderwidth = 0 ,font = ('verdana' ,10 ,'bold' ))
23+ self .p .place (x = 5 ,y = 11 )
24+ self .pen_button = Button (self .paint_tools ,padx = 6 ,image = self .pen_logo ,borderwidth = 2 ,command = self .use_pen )
25+ self .pen_button .place (x = 60 ,y = 10 )
26+
27+ self .brush_logo = ImageTk .PhotoImage (Image .open ('brush.png' ))
28+ self .b = Label (self .paint_tools ,borderwidth = 0 ,text = 'brush' ,font = ('verdana' ,10 ,'bold' ))
29+ self .b .place (x = 5 ,y = 40 )
30+ self .brush_button = Button (self .paint_tools ,image = self .brush_logo ,borderwidth = 2 ,command = self .use_brush )
31+ self .brush_button .place (x = 60 ,y = 40 )
32+
33+ self .color_logo = ImageTk .PhotoImage (Image .open ('color.png' ))
34+ self .cl = Label (self .paint_tools , text = 'color' ,font = ('verdana' ,10 ,'bold' ))
35+ self .cl .place (x = 5 ,y = 70 )
36+ self .color_button = Button (self .paint_tools ,image = self .color_logo ,borderwidth = 2 ,command = self .choose_color )
37+ self .color_button .place (x = 60 ,y = 70 )
38+
39+ self .eraser_logo = ImageTk .PhotoImage (Image .open ('eraser.png' ))
40+ self .e = Label (self .paint_tools , text = 'eraser' ,font = ('verdana' ,10 ,'bold' ))
41+ self .e .place (x = 5 ,y = 100 )
42+ self .eraser_button = Button (self .paint_tools ,image = self .eraser_logo ,borderwidth = 2 ,command = self .use_eraser )
43+ self .eraser_button .place (x = 60 ,y = 100 )
44+
45+ self .pen_size = Label (self .paint_tools ,text = "Pen Size" ,font = ('verdana' ,10 ,'bold' ))
46+ self .pen_size .place (x = 15 ,y = 250 )
47+ self .choose_size_button = Scale (self .paint_tools , from_ = 1 , to = 10 , orient = VERTICAL )
48+ self .choose_size_button .place (x = 20 ,y = 150 )
49+
50+
51+
52+
53+ self .c = Canvas (self .root , bg = 'white' , width = 600 , height = 600 ,relief = RIDGE ,borderwidth = 0 )
54+ self .c .place (x = 100 ,y = 0 )
55+
56+ self .setup ()
57+ self .root .mainloop ()
58+
59+ def setup (self ):
60+ self .old_x = None
61+ self .old_y = None
62+ self .line_width = self .choose_size_button .get ()
63+ self .color = self .DEFAULT_COLOR
64+ self .eraser_on = False
65+ self .active_button = self .pen_button
66+ self .c .bind ('<B1-Motion>' , self .paint )
67+ self .c .bind ('<ButtonRelease-1>' , self .reset )
68+
69+ def use_pen (self ):
70+ self .activate_button (self .pen_button )
71+
72+ def use_brush (self ):
73+ self .activate_button (self .brush_button )
74+
75+ def choose_color (self ):
76+ self .eraser_on = False
77+ self .color = askcolor (color = self .color )[1 ]
78+
79+ def use_eraser (self ):
80+ self .activate_button (self .eraser_button , eraser_mode = True )
81+
82+ def activate_button (self , some_button , eraser_mode = False ):
83+ self .active_button .config (relief = RAISED )
84+ some_button .config (relief = SUNKEN )
85+ self .active_button = some_button
86+ self .eraser_on = eraser_mode
87+
88+ def paint (self , event ):
89+ self .line_width = self .choose_size_button .get ()
90+ paint_color = 'white' if self .eraser_on else self .color
91+ if self .old_x and self .old_y :
92+ self .c .create_line (self .old_x , self .old_y , event .x , event .y ,
93+ width = self .line_width , fill = paint_color ,
94+ capstyle = ROUND , smooth = TRUE , splinesteps = 36 )
95+ self .old_x = event .x
96+ self .old_y = event .y
97+
98+ def reset (self , event ):
99+ self .old_x , self .old_y = None , None
100+
101+
102+ if __name__ == '__main__' :
103+ Paint ()
0 commit comments