3
3
from tkinter import colorchooser , filedialog , messagebox
4
4
import PIL .ImageGrab as ImageGrab
5
5
6
+
6
7
class DrawApp :
7
8
def __init__ (self , root ):
8
9
self .root = root
@@ -13,47 +14,127 @@ def __init__(self, root):
13
14
self .setup_widgets ()
14
15
15
16
def setup_widgets (self ):
16
- self .title_label = tk .Label (self .root , text = "Kids' Paint App" , font = ('Comic Sans MS' , 30 ),
17
- bg = 'lightblue' , fg = 'purple' )
17
+ self .title_label = tk .Label (
18
+ self .root ,
19
+ text = "Kids' Paint App" ,
20
+ font = ("Comic Sans MS" , 30 ),
21
+ bg = "lightblue" ,
22
+ fg = "purple" ,
23
+ )
18
24
self .title_label .pack (fill = tk .X , pady = 10 )
19
- self .color_frame = tk .LabelFrame (self .root , text = 'Colors' , font = ('Comic Sans MS' , 15 ), bd = 5 , relief = tk .RIDGE , bg = "white" )
25
+ self .color_frame = tk .LabelFrame (
26
+ self .root ,
27
+ text = "Colors" ,
28
+ font = ("Comic Sans MS" , 15 ),
29
+ bd = 5 ,
30
+ relief = tk .RIDGE ,
31
+ bg = "white" ,
32
+ )
20
33
self .color_frame .place (x = 10 , y = 80 , width = 90 , height = 180 )
21
- colors = ['blue' , 'red' , 'green' , 'orange' , 'violet' , 'black' , 'yellow' , 'purple' , 'pink' , 'gold' , 'brown' ,
22
- 'indigo' ]
34
+ colors = [
35
+ "blue" ,
36
+ "red" ,
37
+ "green" ,
38
+ "orange" ,
39
+ "violet" ,
40
+ "black" ,
41
+ "yellow" ,
42
+ "purple" ,
43
+ "pink" ,
44
+ "gold" ,
45
+ "brown" ,
46
+ "indigo" ,
47
+ ]
23
48
i , j = 0 , 0
24
49
for color in colors :
25
- tk .Button (self .color_frame , bg = color , bd = 2 , relief = tk .RIDGE , width = 3 ,
26
- command = lambda col = color : self .select_color (col )).grid (row = i , column = j , padx = 2 , pady = 2 )
50
+ tk .Button (
51
+ self .color_frame ,
52
+ bg = color ,
53
+ bd = 2 ,
54
+ relief = tk .RIDGE ,
55
+ width = 3 ,
56
+ command = lambda col = color : self .select_color (col ),
57
+ ).grid (row = i , column = j , padx = 2 , pady = 2 )
27
58
i += 1
28
59
if i == 4 :
29
60
i = 0
30
61
j = 1
31
- self .eraser_btn = tk .Button (self .root , text = "Eraser" , bd = 4 , bg = 'white' , command = self .eraser , width = 9 ,
32
- relief = tk .RIDGE , font = ('Comic Sans MS' , 12 ))
62
+ self .eraser_btn = tk .Button (
63
+ self .root ,
64
+ text = "Eraser" ,
65
+ bd = 4 ,
66
+ bg = "white" ,
67
+ command = self .eraser ,
68
+ width = 9 ,
69
+ relief = tk .RIDGE ,
70
+ font = ("Comic Sans MS" , 12 ),
71
+ )
33
72
self .eraser_btn .place (x = 10 , y = 310 )
34
- self .clear_screen_btn = tk .Button (self .root , text = "Clear Screen" , bd = 4 , bg = 'white' ,
35
- command = self .clear_screen , width = 12 , relief = tk .RIDGE , font = ('Comic Sans MS' , 12 ))
73
+ self .clear_screen_btn = tk .Button (
74
+ self .root ,
75
+ text = "Clear Screen" ,
76
+ bd = 4 ,
77
+ bg = "white" ,
78
+ command = self .clear_screen ,
79
+ width = 12 ,
80
+ relief = tk .RIDGE ,
81
+ font = ("Comic Sans MS" , 12 ),
82
+ )
36
83
self .clear_screen_btn .place (x = 10 , y = 370 )
37
- self .save_as_btn = tk .Button (self .root , text = "Save Drawing" , bd = 4 , bg = 'white' , command = self .save_as , width = 12 ,
38
- relief = tk .RIDGE , font = ('Comic Sans MS' , 12 ))
84
+ self .save_as_btn = tk .Button (
85
+ self .root ,
86
+ text = "Save Drawing" ,
87
+ bd = 4 ,
88
+ bg = "white" ,
89
+ command = self .save_as ,
90
+ width = 12 ,
91
+ relief = tk .RIDGE ,
92
+ font = ("Comic Sans MS" , 12 ),
93
+ )
39
94
self .save_as_btn .place (x = 10 , y = 430 )
40
- self .bg_btn = tk .Button (self .root , text = "Background" , bd = 4 , bg = 'white' , command = self .canvas_color , width = 12 ,
41
- relief = tk .RIDGE , font = ('Comic Sans MS' , 12 ))
95
+ self .bg_btn = tk .Button (
96
+ self .root ,
97
+ text = "Background" ,
98
+ bd = 4 ,
99
+ bg = "white" ,
100
+ command = self .canvas_color ,
101
+ width = 12 ,
102
+ relief = tk .RIDGE ,
103
+ font = ("Comic Sans MS" , 12 ),
104
+ )
42
105
self .bg_btn .place (x = 10 , y = 490 )
43
- self .pointer_frame = tk .LabelFrame (self .root , text = 'Size' , bd = 5 , bg = 'white' , font = ('Comic Sans MS' , 15 , 'bold' ),
44
- relief = tk .RIDGE )
106
+ self .pointer_frame = tk .LabelFrame (
107
+ self .root ,
108
+ text = "Size" ,
109
+ bd = 5 ,
110
+ bg = "white" ,
111
+ font = ("Comic Sans MS" , 15 , "bold" ),
112
+ relief = tk .RIDGE ,
113
+ )
45
114
self .pointer_frame .place (x = 10 , y = 580 , height = 150 , width = 70 )
46
- self .pointer_size = Scale (self .pointer_frame , orient = tk .VERTICAL , from_ = 48 , to = 1 , length = 120 )
115
+ self .pointer_size = Scale (
116
+ self .pointer_frame , orient = tk .VERTICAL , from_ = 48 , to = 1 , length = 120
117
+ )
47
118
self .pointer_size .set (1 )
48
119
self .pointer_size .grid (row = 0 , column = 1 , padx = 15 )
49
- self .canvas = tk .Canvas (self .root , bg = 'white' , bd = 5 , relief = tk .GROOVE , height = 650 , width = 1300 )
120
+ self .canvas = tk .Canvas (
121
+ self .root , bg = "white" , bd = 5 , relief = tk .GROOVE , height = 650 , width = 1300
122
+ )
50
123
self .canvas .place (x = 160 , y = 120 , anchor = "nw" )
51
124
self .canvas .bind ("<B1-Motion>" , self .paint )
52
125
53
126
def paint (self , event ):
54
127
x1 , y1 = (event .x - 2 ), (event .y - 2 )
55
128
x2 , y2 = (event .x + 2 ), (event .y + 2 )
56
- self .canvas .create_oval (x1 , y1 , x2 , y2 , fill = self .pointer , outline = self .pointer , width = self .pointer_size .get ())
129
+ self .canvas .create_oval (
130
+ x1 ,
131
+ y1 ,
132
+ x2 ,
133
+ y2 ,
134
+ fill = self .pointer ,
135
+ outline = self .pointer ,
136
+ width = self .pointer_size .get (),
137
+ )
57
138
58
139
def select_color (self , col ):
59
140
self .pointer = col
@@ -62,7 +143,7 @@ def eraser(self):
62
143
self .pointer = self .erase
63
144
64
145
def clear_screen (self ):
65
- self .canvas .delete (' all' )
146
+ self .canvas .delete (" all" )
66
147
67
148
def canvas_color (self ):
68
149
color = colorchooser .askcolor ()
@@ -71,17 +152,20 @@ def canvas_color(self):
71
152
self .erase = color [1 ]
72
153
73
154
def save_as (self ):
74
- file_path = filedialog .asksaveasfilename (defaultextension = '.jpg' , filetypes = [("Image files" , "*.jpg" )])
155
+ file_path = filedialog .asksaveasfilename (
156
+ defaultextension = ".jpg" , filetypes = [("Image files" , "*.jpg" )]
157
+ )
75
158
if file_path :
76
159
try :
77
160
y = 148
78
161
x = 200
79
162
y1 = 978
80
163
x1 = 1840
81
164
ImageGrab .grab ().crop ((x , y , x1 , y1 )).save (file_path )
82
- messagebox .showinfo (' Save Drawing' , ' Image file saved successfully!' )
165
+ messagebox .showinfo (" Save Drawing" , " Image file saved successfully!" )
83
166
except Exception as e :
84
- messagebox .showerror ('Error' , f"Failed to save the image file: { e } " )
167
+ messagebox .showerror ("Error" , f"Failed to save the image file: { e } " )
168
+
85
169
86
170
if __name__ == "__main__" :
87
171
root = tk .Tk ()
0 commit comments