Skip to content

Commit 3286bd9

Browse files
Update paint_app.py
1 parent 41d2b6e commit 3286bd9

File tree

1 file changed

+108
-24
lines changed

1 file changed

+108
-24
lines changed

paint_app.py

+108-24
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from tkinter import colorchooser, filedialog, messagebox
44
import PIL.ImageGrab as ImageGrab
55

6+
67
class DrawApp:
78
def __init__(self, root):
89
self.root = root
@@ -13,47 +14,127 @@ def __init__(self, root):
1314
self.setup_widgets()
1415

1516
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+
)
1824
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+
)
2033
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+
]
2348
i, j = 0, 0
2449
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)
2758
i += 1
2859
if i == 4:
2960
i = 0
3061
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+
)
3372
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+
)
3683
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+
)
3994
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+
)
42105
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+
)
45114
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+
)
47118
self.pointer_size.set(1)
48119
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+
)
50123
self.canvas.place(x=160, y=120, anchor="nw")
51124
self.canvas.bind("<B1-Motion>", self.paint)
52125

53126
def paint(self, event):
54127
x1, y1 = (event.x - 2), (event.y - 2)
55128
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+
)
57138

58139
def select_color(self, col):
59140
self.pointer = col
@@ -62,7 +143,7 @@ def eraser(self):
62143
self.pointer = self.erase
63144

64145
def clear_screen(self):
65-
self.canvas.delete('all')
146+
self.canvas.delete("all")
66147

67148
def canvas_color(self):
68149
color = colorchooser.askcolor()
@@ -71,17 +152,20 @@ def canvas_color(self):
71152
self.erase = color[1]
72153

73154
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+
)
75158
if file_path:
76159
try:
77160
y = 148
78161
x = 200
79162
y1 = 978
80163
x1 = 1840
81164
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!")
83166
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+
85169

86170
if __name__ == "__main__":
87171
root = tk.Tk()

0 commit comments

Comments
 (0)