|
| 1 | +import sys |
| 2 | +import os |
| 3 | +import threading |
| 4 | +import tkinter as tk |
| 5 | +from tkinter import ttk, messagebox, filedialog |
| 6 | +import sv_ttk |
| 7 | + |
| 8 | +# ========================= |
| 9 | +# Helpers |
| 10 | +# ========================= |
| 11 | +def resource_path(file_name): |
| 12 | + base_path = getattr(sys, "_MEIPASS", os.path.dirname(os.path.abspath(__file__))) |
| 13 | + return os.path.join(base_path, file_name) |
| 14 | + |
| 15 | +# ========================= |
| 16 | +# App Setup |
| 17 | +# ========================= |
| 18 | +root = tk.Tk() |
| 19 | +root.title("FiboTool") |
| 20 | +root.geometry("980x620") |
| 21 | +root.minsize(900, 550) |
| 22 | + |
| 23 | +sv_ttk.set_theme("light") |
| 24 | + |
| 25 | +# ========================= |
| 26 | +# Globals |
| 27 | +# ========================= |
| 28 | +count_var = tk.IntVar(value=100) |
| 29 | +status_var = tk.StringVar(value="Ready") |
| 30 | + |
| 31 | +# ========================= |
| 32 | +# Status |
| 33 | +# ========================= |
| 34 | +def set_status(msg): |
| 35 | + status_var.set(msg) |
| 36 | + root.update_idletasks() |
| 37 | + |
| 38 | +# ========================= |
| 39 | +# Fibonacci Logic (STREAMED) |
| 40 | +# ========================= |
| 41 | +def generate_fibonacci(): |
| 42 | + try: |
| 43 | + n = count_var.get() |
| 44 | + if n <= 0 or n > 100000: |
| 45 | + raise ValueError |
| 46 | + except Exception: |
| 47 | + messagebox.showerror("Invalid Input", "Enter a number between 1 and 100,000.") |
| 48 | + return |
| 49 | + |
| 50 | + output.delete("1.0", tk.END) |
| 51 | + progress["value"] = 0 |
| 52 | + progress["maximum"] = n |
| 53 | + set_status("Generating sequence...") |
| 54 | + |
| 55 | + def task(): |
| 56 | + a, b = 0, 1 |
| 57 | + chunk = [] |
| 58 | + |
| 59 | + for i in range(n): |
| 60 | + chunk.append(str(a)) |
| 61 | + a, b = b, a + b |
| 62 | + |
| 63 | + if i % 100 == 0: |
| 64 | + text = ", ".join(chunk) + ", " |
| 65 | + root.after(0, output.insert, tk.END, text) |
| 66 | + root.after(0, progress.step, 100) |
| 67 | + chunk.clear() |
| 68 | + |
| 69 | + if chunk: |
| 70 | + root.after(0, output.insert, tk.END, ", ".join(chunk)) |
| 71 | + |
| 72 | + root.after(0, lambda: progress.config(value=n)) |
| 73 | + root.after(0, set_status, f"Generated {n} terms") |
| 74 | + |
| 75 | + threading.Thread(target=task, daemon=True).start() |
| 76 | + |
| 77 | +def copy_result(): |
| 78 | + root.clipboard_clear() |
| 79 | + root.clipboard_append(output.get("1.0", tk.END)) |
| 80 | + set_status("Copied to clipboard") |
| 81 | + |
| 82 | +def export_txt(): |
| 83 | + file_path = filedialog.asksaveasfilename( |
| 84 | + defaultextension=".txt", |
| 85 | + filetypes=[("Text Files", "*.txt")] |
| 86 | + ) |
| 87 | + if not file_path: |
| 88 | + return |
| 89 | + |
| 90 | + with open(file_path, "w", encoding="utf-8") as f: |
| 91 | + f.write(output.get("1.0", tk.END)) |
| 92 | + |
| 93 | + set_status(f"Saved to {os.path.basename(file_path)}") |
| 94 | + |
| 95 | +# ========================= |
| 96 | +# Styles |
| 97 | +# ========================= |
| 98 | +style = ttk.Style() |
| 99 | +style.configure("Title.TLabel", font=("Segoe UI", 24, "bold")) |
| 100 | +style.configure("Subtitle.TLabel", font=("Segoe UI", 11)) |
| 101 | +style.configure("Action.TButton", font=("Segoe UI", 11, "bold"), padding=10) |
| 102 | + |
| 103 | +# ========================= |
| 104 | +# Layout (GRID-BASED) |
| 105 | +# ========================= |
| 106 | +root.columnconfigure(0, weight=1) |
| 107 | +root.rowconfigure(1, weight=1) |
| 108 | + |
| 109 | +# ----- Header ----- |
| 110 | +header = ttk.Frame(root, padding=(24, 16)) |
| 111 | +header.grid(row=0, column=0, sticky="ew") |
| 112 | + |
| 113 | +header.columnconfigure(1, weight=1) |
| 114 | + |
| 115 | +ttk.Label(header, text="FiboTool", style="Title.TLabel").grid(row=0, column=0, sticky="w") |
| 116 | +ttk.Label( |
| 117 | + header, |
| 118 | + text="High-performance Fibonacci sequence generator", |
| 119 | + style="Subtitle.TLabel" |
| 120 | +).grid(row=1, column=0, sticky="w", pady=(2, 0)) |
| 121 | + |
| 122 | +controls = ttk.Frame(header) |
| 123 | +controls.grid(row=0, column=1, rowspan=2, sticky="e") |
| 124 | + |
| 125 | +ttk.Label(controls, text="Terms").grid(row=0, column=0, padx=6) |
| 126 | +ttk.Entry(controls, textvariable=count_var, width=10).grid(row=0, column=1) |
| 127 | + |
| 128 | +# ----- Main Content ----- |
| 129 | +main = ttk.Frame(root, padding=(24, 0, 24, 16)) |
| 130 | +main.grid(row=1, column=0, sticky="nsew") |
| 131 | +main.columnconfigure(0, weight=1) |
| 132 | +main.rowconfigure(0, weight=1) |
| 133 | + |
| 134 | +output_card = ttk.LabelFrame(main, text="Output", padding=10) |
| 135 | +output_card.grid(row=0, column=0, sticky="nsew") |
| 136 | +output_card.columnconfigure(0, weight=1) |
| 137 | +output_card.rowconfigure(0, weight=1) |
| 138 | + |
| 139 | +scroll = ttk.Scrollbar(output_card) |
| 140 | +scroll.grid(row=0, column=1, sticky="ns") |
| 141 | + |
| 142 | +output = tk.Text( |
| 143 | + output_card, |
| 144 | + wrap="word", |
| 145 | + font=("Consolas", 10), |
| 146 | + yscrollcommand=scroll.set |
| 147 | +) |
| 148 | +output.grid(row=0, column=0, sticky="nsew") |
| 149 | +scroll.config(command=output.yview) |
| 150 | + |
| 151 | +# ----- Actions ----- |
| 152 | +actions = ttk.Frame(main) |
| 153 | +actions.grid(row=1, column=0, pady=12, sticky="ew") |
| 154 | + |
| 155 | +ttk.Button( |
| 156 | + actions, |
| 157 | + text="Generate", |
| 158 | + command=generate_fibonacci, |
| 159 | + style="Action.TButton" |
| 160 | +).pack(side="left") |
| 161 | + |
| 162 | +ttk.Button(actions, text="Copy", command=copy_result).pack(side="left", padx=6) |
| 163 | +ttk.Button(actions, text="Export TXT", command=export_txt).pack(side="left") |
| 164 | + |
| 165 | +progress = ttk.Progressbar(actions) |
| 166 | +progress.pack(side="right", fill="x", expand=True, padx=(12, 0)) |
| 167 | + |
| 168 | +# ----- Status Bar ----- |
| 169 | +status = ttk.Label(root, textvariable=status_var, anchor="w", padding=6) |
| 170 | +status.grid(row=2, column=0, sticky="ew") |
| 171 | + |
| 172 | +# ========================= |
| 173 | +# Run |
| 174 | +# ========================= |
| 175 | +root.mainloop() |
0 commit comments