|
| 1 | +import tkinter as tk |
| 2 | +from tkinter.ttk import Scale |
| 3 | +from tkinter import colorchooser, filedialog, messagebox |
| 4 | +import PIL.ImageGrab as ImageGrab |
| 5 | + |
| 6 | +class DrawApp: |
| 7 | + def __init__(self, root): |
| 8 | + self.root = root |
| 9 | + self.root.title("Kids' Paint App") |
| 10 | + self.root.attributes("-fullscreen", True) |
| 11 | + self.pointer = "black" |
| 12 | + self.erase = "white" |
| 13 | + self.setup_widgets() |
| 14 | + |
| 15 | + 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') |
| 18 | + 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") |
| 20 | + 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'] |
| 23 | + i, j = 0, 0 |
| 24 | + 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) |
| 27 | + i += 1 |
| 28 | + if i == 4: |
| 29 | + i = 0 |
| 30 | + 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)) |
| 33 | + 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)) |
| 36 | + 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)) |
| 39 | + 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)) |
| 42 | + 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) |
| 45 | + 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) |
| 47 | + self.pointer_size.set(1) |
| 48 | + 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) |
| 50 | + self.canvas.place(x=160, y=120, anchor="nw") |
| 51 | + self.canvas.bind("<B1-Motion>", self.paint) |
| 52 | + |
| 53 | + def paint(self, event): |
| 54 | + x1, y1 = (event.x - 2), (event.y - 2) |
| 55 | + 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()) |
| 57 | + |
| 58 | + def select_color(self, col): |
| 59 | + self.pointer = col |
| 60 | + |
| 61 | + def eraser(self): |
| 62 | + self.pointer = self.erase |
| 63 | + |
| 64 | + def clear_screen(self): |
| 65 | + self.canvas.delete('all') |
| 66 | + |
| 67 | + def canvas_color(self): |
| 68 | + color = colorchooser.askcolor() |
| 69 | + if color: |
| 70 | + self.canvas.configure(background=color[1]) |
| 71 | + self.erase = color[1] |
| 72 | + |
| 73 | + def save_as(self): |
| 74 | + file_path = filedialog.asksaveasfilename(defaultextension='.jpg', filetypes=[("Image files", "*.jpg")]) |
| 75 | + if file_path: |
| 76 | + try: |
| 77 | + y = 148 |
| 78 | + x = 200 |
| 79 | + y1 = 978 |
| 80 | + x1 = 1840 |
| 81 | + ImageGrab.grab().crop((x, y, x1, y1)).save(file_path) |
| 82 | + messagebox.showinfo('Save Drawing', 'Image file saved successfully!') |
| 83 | + except Exception as e: |
| 84 | + messagebox.showerror('Error', f"Failed to save the image file: {e}") |
| 85 | + |
| 86 | +if __name__ == "__main__": |
| 87 | + root = tk.Tk() |
| 88 | + app = DrawApp(root) |
| 89 | + root.mainloop() |
0 commit comments