|
| 1 | +import tkinter as tk |
| 2 | +from tkinter import colorchooser |
| 3 | +from abc import ABC, abstractmethod |
| 4 | + |
| 5 | +# Shape interface |
| 6 | +class Shape(ABC): |
| 7 | + @abstractmethod |
| 8 | + def draw(self, canvas, **kwargs): |
| 9 | + pass |
| 10 | + |
| 11 | +class Circle(Shape): |
| 12 | + def draw(self, canvas, center, radius, fill, outline, width): |
| 13 | + x0 = center[0] - radius |
| 14 | + y0 = center[1] - radius |
| 15 | + x1 = center[0] + radius |
| 16 | + y1 = center[1] + radius |
| 17 | + canvas.create_oval(x0, y0, x1, y1, fill=fill, outline=outline, width=width) |
| 18 | + |
| 19 | +class Square(Shape): |
| 20 | + def draw(self, canvas, center, side, fill, outline, width): |
| 21 | + half = side // 2 |
| 22 | + x0 = center[0] - half |
| 23 | + y0 = center[1] - half |
| 24 | + x1 = center[0] + half |
| 25 | + y1 = center[1] + half |
| 26 | + canvas.create_rectangle(x0, y0, x1, y1, fill=fill, outline=outline, width=width) |
| 27 | + |
| 28 | +class Rectangle(Shape): |
| 29 | + def draw(self, canvas, center, width_r, height_r, fill, outline, width): |
| 30 | + x0 = center[0] - width_r // 2 |
| 31 | + y0 = center[1] - height_r // 2 |
| 32 | + x1 = center[0] + width_r // 2 |
| 33 | + y1 = center[1] + height_r // 2 |
| 34 | + canvas.create_rectangle(x0, y0, x1, y1, fill=fill, outline=outline, width=width) |
| 35 | + |
| 36 | +class ShapeFactory: |
| 37 | + @staticmethod |
| 38 | + def create_shape(shape_type): |
| 39 | + shape_type = shape_type.lower() |
| 40 | + if shape_type == 'circle': |
| 41 | + return Circle() |
| 42 | + elif shape_type == 'square': |
| 43 | + return Square() |
| 44 | + elif shape_type == 'rectangle': |
| 45 | + return Rectangle() |
| 46 | + else: |
| 47 | + raise ValueError(f"Unknown shape type: {shape_type}") |
| 48 | + |
| 49 | +class AdvancedShapeApp: |
| 50 | + def __init__(self, root): |
| 51 | + self.root = root |
| 52 | + self.root.title("Advanced Shape Factory GUI") |
| 53 | + self.root.geometry("600x500") |
| 54 | + self.root.resizable(False, False) |
| 55 | + self.bg_color = "#f2f2f2" |
| 56 | + self.selected_color = "#3498db" |
| 57 | + self.outline_color = "#222" |
| 58 | + self.outline_width = 3 |
| 59 | + |
| 60 | + # Controls Frame |
| 61 | + self.controls = tk.Frame(root, bg="#e8eaf6", bd=2, relief=tk.GROOVE) |
| 62 | + self.controls.place(x=10, y=10, width=220, height=480) |
| 63 | + |
| 64 | + tk.Label(self.controls, text="Shape Type", bg="#e8eaf6", font=("Segoe UI", 11, "bold")).pack(pady=7) |
| 65 | + self.shape_var = tk.StringVar(value="circle") |
| 66 | + shapes = ["circle", "square", "rectangle"] |
| 67 | + for s in shapes: |
| 68 | + tk.Radiobutton(self.controls, text=s.title(), variable=self.shape_var, value=s, bg="#e8eaf6", font=("Segoe UI", 10)).pack(anchor=tk.W) |
| 69 | + |
| 70 | + # Size controls (dynamic) |
| 71 | + self.size_frame = tk.Frame(self.controls, bg="#e8eaf6") |
| 72 | + self.size_frame.pack(pady=8) |
| 73 | + self.size_labels = {} |
| 74 | + self.size_entries = {} |
| 75 | + self._create_size_controls() |
| 76 | + self.shape_var.trace_add('write', lambda *args: self._update_size_controls()) |
| 77 | + |
| 78 | + # Color picker |
| 79 | + tk.Label(self.controls, text="Fill Color", bg="#e8eaf6", font=("Segoe UI", 11, "bold")).pack(pady=(12, 3)) |
| 80 | + self.color_btn = tk.Button(self.controls, text="Choose Color", command=self.choose_color, bg=self.selected_color, fg="white", font=("Segoe UI", 10, "bold")) |
| 81 | + self.color_btn.pack(pady=2) |
| 82 | + |
| 83 | + # Outline width |
| 84 | + tk.Label(self.controls, text="Outline Width", bg="#e8eaf6", font=("Segoe UI", 11, "bold")).pack(pady=(10, 3)) |
| 85 | + self.outline_entry = tk.Entry(self.controls, width=5, font=("Segoe UI", 10)) |
| 86 | + self.outline_entry.insert(0, str(self.outline_width)) |
| 87 | + self.outline_entry.pack(pady=2) |
| 88 | + |
| 89 | + # Draw & Clear |
| 90 | + self.draw_btn = tk.Button(self.controls, text="Draw Shape", command=self.draw_shape, bg="#43a047", fg="white", font=("Segoe UI", 11, "bold")) |
| 91 | + self.draw_btn.pack(pady=(16, 5), fill=tk.X) |
| 92 | + self.clear_btn = tk.Button(self.controls, text="Clear Canvas", command=self.clear_canvas, bg="#e53935", fg="white", font=("Segoe UI", 11, "bold")) |
| 93 | + self.clear_btn.pack(pady=2, fill=tk.X) |
| 94 | + |
| 95 | + # Canvas |
| 96 | + self.canvas = tk.Canvas(root, width=340, height=460, bg=self.bg_color, highlightthickness=2, highlightbackground="#888") |
| 97 | + self.canvas.place(x=250, y=15) |
| 98 | + |
| 99 | + # Status |
| 100 | + self.status_label = tk.Label(root, text="", fg="red", font=("Segoe UI", 10)) |
| 101 | + self.status_label.place(x=250, y=480) |
| 102 | + |
| 103 | + def _create_size_controls(self): |
| 104 | + # Remove old |
| 105 | + for widget in self.size_frame.winfo_children(): |
| 106 | + widget.destroy() |
| 107 | + self.size_labels.clear() |
| 108 | + self.size_entries.clear() |
| 109 | + # Create for default (circle) |
| 110 | + self._update_size_controls() |
| 111 | + |
| 112 | + def _update_size_controls(self): |
| 113 | + for widget in self.size_frame.winfo_children(): |
| 114 | + widget.destroy() |
| 115 | + shape = self.shape_var.get() |
| 116 | + if shape == "circle": |
| 117 | + self.size_labels['radius'] = tk.Label(self.size_frame, text="Radius:", bg="#e8eaf6", font=("Segoe UI", 10)) |
| 118 | + self.size_labels['radius'].grid(row=0, column=0, sticky=tk.W) |
| 119 | + self.size_entries['radius'] = tk.Entry(self.size_frame, width=8, font=("Segoe UI", 10)) |
| 120 | + self.size_entries['radius'].insert(0, "60") |
| 121 | + self.size_entries['radius'].grid(row=0, column=1) |
| 122 | + elif shape == "square": |
| 123 | + self.size_labels['side'] = tk.Label(self.size_frame, text="Side:", bg="#e8eaf6", font=("Segoe UI", 10)) |
| 124 | + self.size_labels['side'].grid(row=0, column=0, sticky=tk.W) |
| 125 | + self.size_entries['side'] = tk.Entry(self.size_frame, width=8, font=("Segoe UI", 10)) |
| 126 | + self.size_entries['side'].insert(0, "100") |
| 127 | + self.size_entries['side'].grid(row=0, column=1) |
| 128 | + elif shape == "rectangle": |
| 129 | + self.size_labels['width'] = tk.Label(self.size_frame, text="Width:", bg="#e8eaf6", font=("Segoe UI", 10)) |
| 130 | + self.size_labels['width'].grid(row=0, column=0, sticky=tk.W) |
| 131 | + self.size_entries['width'] = tk.Entry(self.size_frame, width=8, font=("Segoe UI", 10)) |
| 132 | + self.size_entries['width'].insert(0, "150") |
| 133 | + self.size_entries['width'].grid(row=0, column=1) |
| 134 | + self.size_labels['height'] = tk.Label(self.size_frame, text="Height:", bg="#e8eaf6", font=("Segoe UI", 10)) |
| 135 | + self.size_labels['height'].grid(row=1, column=0, sticky=tk.W) |
| 136 | + self.size_entries['height'] = tk.Entry(self.size_frame, width=8, font=("Segoe UI", 10)) |
| 137 | + self.size_entries['height'].insert(0, "80") |
| 138 | + self.size_entries['height'].grid(row=1, column=1) |
| 139 | + |
| 140 | + def choose_color(self): |
| 141 | + color = colorchooser.askcolor(title="Choose Fill Color", initialcolor=self.selected_color) |
| 142 | + if color[1]: |
| 143 | + self.selected_color = color[1] |
| 144 | + self.color_btn.config(bg=self.selected_color) |
| 145 | + |
| 146 | + def draw_shape(self): |
| 147 | + shape_type = self.shape_var.get() |
| 148 | + try: |
| 149 | + outline_width = int(self.outline_entry.get()) |
| 150 | + except Exception: |
| 151 | + self.status_label.config(text="Outline width must be a number.", fg="red") |
| 152 | + return |
| 153 | + # Get size |
| 154 | + try: |
| 155 | + if shape_type == "circle": |
| 156 | + radius = int(self.size_entries['radius'].get()) |
| 157 | + shape = ShapeFactory.create_shape(shape_type) |
| 158 | + shape.draw(self.canvas, center=(170, 230), radius=radius, fill=self.selected_color, outline=self.outline_color, width=outline_width) |
| 159 | + elif shape_type == "square": |
| 160 | + side = int(self.size_entries['side'].get()) |
| 161 | + shape = ShapeFactory.create_shape(shape_type) |
| 162 | + shape.draw(self.canvas, center=(170, 230), side=side, fill=self.selected_color, outline=self.outline_color, width=outline_width) |
| 163 | + elif shape_type == "rectangle": |
| 164 | + width_r = int(self.size_entries['width'].get()) |
| 165 | + height_r = int(self.size_entries['height'].get()) |
| 166 | + shape = ShapeFactory.create_shape(shape_type) |
| 167 | + shape.draw(self.canvas, center=(170, 230), width_r=width_r, height_r=height_r, fill=self.selected_color, outline=self.outline_color, width=outline_width) |
| 168 | + self.status_label.config(text=f"Drew a {shape_type}.", fg="green") |
| 169 | + except Exception as e: |
| 170 | + self.status_label.config(text=f"Error: {e}", fg="red") |
| 171 | + |
| 172 | + def clear_canvas(self): |
| 173 | + self.canvas.delete("all") |
| 174 | + self.status_label.config(text="Canvas cleared.", fg="blue") |
| 175 | + |
| 176 | +if __name__ == "__main__": |
| 177 | + root = tk.Tk() |
| 178 | + app = AdvancedShapeApp(root) |
| 179 | + root.mainloop() |
0 commit comments