|
| 1 | +import sys |
| 2 | +import os |
| 3 | +import tkinter as tk |
| 4 | +from tkinter import ttk, messagebox, filedialog |
| 5 | +import sv_ttk |
| 6 | + |
| 7 | +# ========================= |
| 8 | +# Helpers |
| 9 | +# ========================= |
| 10 | +def resource_path(file_name): |
| 11 | + base_path = getattr(sys, "_MEIPASS", os.path.dirname(os.path.abspath(__file__))) |
| 12 | + return os.path.join(base_path, file_name) |
| 13 | + |
| 14 | +def set_status(msg): |
| 15 | + status_var.set(msg) |
| 16 | + root.update_idletasks() |
| 17 | + |
| 18 | +# ========================= |
| 19 | +# App Setup |
| 20 | +# ========================= |
| 21 | +root = tk.Tk() |
| 22 | +root.title("Pattern History Printer") |
| 23 | +root.geometry("600x700") |
| 24 | +root.minsize(600, 700) |
| 25 | +sv_ttk.set_theme("light") |
| 26 | + |
| 27 | +# ========================= |
| 28 | +# Globals |
| 29 | +# ========================= |
| 30 | +dark_mode_var = tk.BooleanVar(value=False) |
| 31 | +rows_var = tk.StringVar(value="5") |
| 32 | +pattern_type_var = tk.StringVar(value="Pyramid") |
| 33 | +pattern_history = [] # List of tuples: (pattern_type, rows, pattern_str) |
| 34 | + |
| 35 | +# ========================= |
| 36 | +# Theme Toggle |
| 37 | +# ========================= |
| 38 | +def toggle_theme(): |
| 39 | + bg = "#2E2E2E" if dark_mode_var.get() else "#FFFFFF" |
| 40 | + fg = "white" if dark_mode_var.get() else "black" |
| 41 | + root.configure(bg=bg) |
| 42 | + for w in ["TFrame", "TLabel", "TLabelframe", "TLabelframe.Label", "TCheckbutton"]: |
| 43 | + style.configure(w, background=bg, foreground=fg) |
| 44 | + for entry in [rows_entry]: |
| 45 | + entry.configure(background=bg, foreground=fg) |
| 46 | + |
| 47 | +# ========================= |
| 48 | +# Pattern Generation |
| 49 | +# ========================= |
| 50 | +def generate_pattern(): |
| 51 | + try: |
| 52 | + rows = int(rows_var.get()) |
| 53 | + if rows < 1: |
| 54 | + raise ValueError |
| 55 | + except ValueError: |
| 56 | + messagebox.showerror("Invalid Input", "Please enter a valid positive integer for rows.") |
| 57 | + return |
| 58 | + |
| 59 | + p_type = pattern_type_var.get() |
| 60 | + pattern_lines = [] |
| 61 | + |
| 62 | + if p_type == "Pyramid": |
| 63 | + for i in range(1, rows + 1): |
| 64 | + line = " " * (rows - i) + "*" * (2 * i - 1) |
| 65 | + pattern_lines.append(line) |
| 66 | + elif p_type == "Right Triangle": |
| 67 | + for i in range(1, rows + 1): |
| 68 | + pattern_lines.append("*" * i) |
| 69 | + elif p_type == "Inverted Pyramid": |
| 70 | + for i in range(rows, 0, -1): |
| 71 | + line = " " * (rows - i) + "*" * (2 * i - 1) |
| 72 | + pattern_lines.append(line) |
| 73 | + else: |
| 74 | + pattern_lines.append("Unknown pattern type!") |
| 75 | + |
| 76 | + pattern_str = "\n".join(pattern_lines) |
| 77 | + add_to_history(p_type, rows, pattern_str) |
| 78 | + set_status("Pattern generated! Click a history entry to view details.") |
| 79 | + |
| 80 | +# ========================= |
| 81 | +# History Management |
| 82 | +# ========================= |
| 83 | +def add_to_history(p_type, rows, pattern_str): |
| 84 | + pattern_history.append((p_type, rows, pattern_str)) |
| 85 | + preview = f"{p_type} | Rows: {rows}" |
| 86 | + history_list.insert(tk.END, preview) |
| 87 | + |
| 88 | +def export_history_txt(): |
| 89 | + if not pattern_history: |
| 90 | + messagebox.showinfo("Empty History", "No patterns to export.") |
| 91 | + return |
| 92 | + |
| 93 | + file_path = filedialog.asksaveasfilename( |
| 94 | + defaultextension=".txt", |
| 95 | + filetypes=[("Text Files", "*.txt")], |
| 96 | + title="Export Pattern History" |
| 97 | + ) |
| 98 | + if not file_path: |
| 99 | + return |
| 100 | + |
| 101 | + try: |
| 102 | + with open(file_path, "w", encoding="utf-8") as f: |
| 103 | + f.write("Pattern History\n") |
| 104 | + f.write("=" * 40 + "\n\n") |
| 105 | + for i, (ptype, rows, pstr) in enumerate(pattern_history, 1): |
| 106 | + f.write(f"{i}. {ptype} | Rows: {rows}\n") |
| 107 | + f.write(pstr + "\n\n") |
| 108 | + set_status("History exported successfully") |
| 109 | + messagebox.showinfo("Export Successful", "Pattern history saved.") |
| 110 | + except Exception as e: |
| 111 | + messagebox.showerror("Export Failed", str(e)) |
| 112 | + |
| 113 | +# ========================= |
| 114 | +# History Viewer |
| 115 | +# ========================= |
| 116 | +def view_selected_history(event=None): |
| 117 | + selection = history_list.curselection() |
| 118 | + if not selection: |
| 119 | + messagebox.showinfo("No Selection", "Please select a pattern from the history.") |
| 120 | + return |
| 121 | + index = selection[0] |
| 122 | + p_type, rows, pattern_str = pattern_history[index] |
| 123 | + |
| 124 | + history_window = tk.Toplevel(root) |
| 125 | + history_window.title(f"{p_type} | Rows: {rows}") |
| 126 | + history_window.geometry("500x400") |
| 127 | + history_window.grab_set() |
| 128 | + |
| 129 | + frame = ttk.Frame(history_window, padding=10) |
| 130 | + frame.pack(expand=True, fill="both") |
| 131 | + |
| 132 | + text_widget = tk.Text(frame, wrap="none", font=("Consolas", 12)) |
| 133 | + text_widget.pack(expand=True, fill="both") |
| 134 | + scrollbar = ttk.Scrollbar(frame, orient="vertical", command=text_widget.yview) |
| 135 | + scrollbar.pack(side="right", fill="y") |
| 136 | + text_widget.configure(yscrollcommand=scrollbar.set) |
| 137 | + |
| 138 | + text_widget.insert(tk.END, pattern_str) |
| 139 | + text_widget.configure(state="disabled") |
| 140 | + history_window.focus() |
| 141 | + |
| 142 | +# ========================= |
| 143 | +# Styles |
| 144 | +# ========================= |
| 145 | +style = ttk.Style() |
| 146 | +style.theme_use("clam") |
| 147 | +style.configure("Action.TButton", font=("Segoe UI", 11, "bold"), padding=8) |
| 148 | + |
| 149 | +# ========================= |
| 150 | +# Status Bar |
| 151 | +# ========================= |
| 152 | +status_var = tk.StringVar(value="Ready") |
| 153 | +ttk.Label(root, textvariable=status_var, anchor="w").pack(side=tk.BOTTOM, fill="x") |
| 154 | + |
| 155 | +# ========================= |
| 156 | +# UI Layout |
| 157 | +# ========================= |
| 158 | +main = ttk.Frame(root, padding=20) |
| 159 | +main.pack(expand=True, fill="both") |
| 160 | +main.columnconfigure(0, weight=1) |
| 161 | + |
| 162 | +ttk.Label(main, text="Pattern History Printer", font=("Segoe UI", 22, "bold")).grid(row=0, column=0, sticky="ew", pady=(0,10)) |
| 163 | + |
| 164 | +# Inputs |
| 165 | +ttk.Label(main, text="Number of Rows:", font=("Segoe UI", 12)).grid(row=1, column=0, sticky="w") |
| 166 | +rows_entry = ttk.Entry(main, textvariable=rows_var, font=("Segoe UI", 14)) |
| 167 | +rows_entry.grid(row=2, column=0, sticky="ew", pady=2) |
| 168 | + |
| 169 | +ttk.Label(main, text="Pattern Type:", font=("Segoe UI", 12)).grid(row=3, column=0, sticky="w") |
| 170 | +pattern_combo = ttk.Combobox(main, textvariable=pattern_type_var, values=["Pyramid", "Right Triangle", "Inverted Pyramid"], font=("Segoe UI", 14), state="readonly") |
| 171 | +pattern_combo.grid(row=4, column=0, sticky="ew", pady=2) |
| 172 | + |
| 173 | +# Controls |
| 174 | +controls = ttk.Frame(main) |
| 175 | +controls.grid(row=5, column=0, sticky="ew", pady=12) |
| 176 | +controls.columnconfigure((0,1), weight=1) |
| 177 | +ttk.Button(controls, text="✨ Generate Pattern", command=generate_pattern, style="Action.TButton").grid(row=0, column=0, padx=4, sticky="ew") |
| 178 | +ttk.Button(controls, text="📤 Export History", command=export_history_txt, style="Action.TButton").grid(row=0, column=1, padx=4, sticky="ew") |
| 179 | + |
| 180 | +# History Vault |
| 181 | +vault = ttk.LabelFrame(main, text="Pattern History Vault", padding=10) |
| 182 | +vault.grid(row=6, column=0, sticky="nsew", pady=10) |
| 183 | +main.rowconfigure(6, weight=1) |
| 184 | +history_list = tk.Listbox(vault, font=("Segoe UI", 10), height=15) |
| 185 | +history_list.pack(fill="both", expand=True) |
| 186 | +history_list.bind("<Double-Button-1>", view_selected_history) |
| 187 | + |
| 188 | +# Options |
| 189 | +options_frame = ttk.Frame(main) |
| 190 | +options_frame.grid(row=7, column=0, sticky="w", pady=6) |
| 191 | +ttk.Checkbutton(options_frame, text="Dark Mode", variable=dark_mode_var, command=toggle_theme).pack(side="left", padx=(0,10)) |
| 192 | + |
| 193 | +# ========================= |
| 194 | +# Run App |
| 195 | +# ========================= |
| 196 | +root.mainloop() |
0 commit comments