welcom ! Handel home

2025年4月10日 星期四

一鍵 .py → .pyc 編譯工具(GUI 版本)

 這裡是 GUI 版本的一鍵 .py → .pyc 編譯工具,使用 tkinter 製作簡單介面,讓你可以點選資料夾後自動編譯所有 .py 檔案。


🖼️ GUI 版一鍵 .py 編譯器(使用 compileall

📄 pyc_gui_compiler.py

python
import tkinter as tk from tkinter import filedialog, messagebox import compileall import os def select_folder(): folder_path = filedialog.askdirectory() if not folder_path: return result = compileall.compile_dir(folder_path, force=True) if result: messagebox.showinfo("完成", f"✅ 已成功編譯:\n{folder_path}") else: messagebox.showerror("錯誤", f"❌ 編譯失敗,請檢查檔案內容\n{folder_path}") # 建立視窗 root = tk.Tk() root.title("Python 一鍵 .py → .pyc 編譯器") root.geometry("400x200") root.resizable(False, False) label = tk.Label(root, text="請選擇含有 Python 程式碼的資料夾", font=("Arial", 12)) label.pack(pady=20) button = tk.Button(root, text="選擇資料夾並開始編譯", command=select_folder, font=("Arial", 12), bg="#4CAF50", fg="white") button.pack(pady=10) footer = tk.Label(root, text="By ChatGPT|使用 compileall 編譯", font=("Arial", 9), fg="gray") footer.pack(side="bottom", pady=10) root.mainloop()

✅ 使用方式

1. 儲存上面程式為 pyc_gui_compiler.py

2. 執行:

bash
python pyc_gui_compiler.py

3. 點選資料夾,即會自動編譯裡面的 .py 檔案成 .pyc


🚀 若想打包成獨立 EXE(不需裝 Python)

pyinstaller 打包:

bash
pyinstaller -F -w pyc_gui_compiler.py
  • -F:打包成單一 EXE

  • -w:不顯示命令列視窗(適合 GUI)

產出後你會在 dist/pyc_gui_compiler.exe 找到它!



沒有留言: