PyInstaller打包工具
使用繁體中文,在w11下,使用UI界面,PyInstaller打包工具,要如何設計,請給可以實際使用的示例
pip install pyinstaller auto-py-to-exe
auto-py-to-exe
當 auto-py-to-exe 打開後,你會看到一個用戶界面,裡面有幾個選項:
Script Location:選擇你要打包的 Python 腳本的路徑。
One Directory 或 One File:選擇打包成單個可執行檔還是文件夾。
Console Window:選擇是否顯示控制台窗口(通常對於 GUI 應用選擇隱藏)。
Icon:如果你想為可執行文件添加圖標,可以在這裡選擇一個 .ico 文件。
你還可以在下方的選項中添加額外的文件或資料夾,修改 Python 版本,並選擇其他設置(例如打包依賴)。
# hello.py
import tkinter as tk
def greet():
print("Hello, World!")
root = tk.Tk()
root.title("Hello App")
button = tk.Button(root, text="Greet", command=greet)
button.pack(pady=20)
root.mainloop()
打包第一版
import tkinter as tk
from tkinter import filedialog, messagebox
import subprocess
def browse_python_file():
file_path = filedialog.askopenfilename(filetypes=[("Python Files", "*.py")])
python_file_entry.delete(0, tk.END)
python_file_entry.insert(0, file_path)
def browse_icon_file():
file_path = filedialog.askopenfilename(filetypes=[("Icon Files", "*.ico")])
icon_file_entry.delete(0, tk.END)
icon_file_entry.insert(0, file_path)
def browse_binary_file():
file_path = filedialog.askopenfilename(filetypes=[("All Files", "*.*")])
binary_file_entry.delete(0, tk.END)
binary_file_entry.insert(0, file_path)
def start_packaging():
python_file = python_file_entry.get()
icon_file = icon_file_entry.get()
binary_file = binary_file_entry.get()
windowed_mode = windowed_var.get()
no_console_mode = no_console_var.get()
if not python_file:
messagebox.showerror("錯誤", "請選擇 Python 檔案")
return
# 建立 PyInstaller 命令
command = ['pyinstaller', '--onefile', python_file] # 增加 --onefile 選項
if icon_file:
command += ['--icon', icon_file]
if binary_file:
command += ['--add-binary', binary_file]
if windowed_mode:
command.append('--windowed')
if no_console_mode:
command.append('--noconsole')
print(f"執行的命令: {' '.join(command)}")
try:
# 使用 Popen 非同步執行,避免卡住
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
stdout, stderr = process.communicate()
if process.returncode == 0:
messagebox.showinfo("成功", f"打包完成!\n{stdout}")
else:
messagebox.showerror("錯誤", f"打包失敗!\n{stderr}")
except Exception as e:
messagebox.showerror("錯誤", f"發生錯誤!\n{str(e)}")
# 設置主窗口
root = tk.Tk()
root.title("PyInstaller 打包工具 by WCS 2024.10.07")
# Python文件選擇
tk.Label(root, text="選擇 Python 檔案:").pack(pady=5)
python_file_entry = tk.Entry(root, width=50)
python_file_entry.pack(pady=5)
tk.Button(root, text="瀏覽", command=browse_python_file).pack()
# 圖標文件選擇
tk.Label(root, text="選擇圖標檔案 (可選):").pack(pady=5)
icon_file_entry = tk.Entry(root, width=50)
icon_file_entry.pack(pady=5)
tk.Button(root, text="瀏覽", command=browse_icon_file).pack()
# Binary文件選擇
tk.Label(root, text="選擇要添加的 Binary 檔案 (可選):").pack(pady=5)
binary_file_entry = tk.Entry(root, width=50)
binary_file_entry.pack(pady=5)
tk.Button(root, text="瀏覽", command=browse_binary_file).pack()
# Windowed 模式選項
windowed_var = tk.BooleanVar()
tk.Checkbutton(root, text="使用 Windowed 模式", variable=windowed_var).pack(pady=5)
# No Console 模式選項
no_console_var = tk.BooleanVar()
tk.Checkbutton(root, text="使用 No Console 模式", variable=no_console_var).pack(pady=5)
# 開始打包按鈕
tk.Button(root, text="開始打包", command=start_packaging).pack(pady=20)
root.mainloop()
打包工具2
-不會呈現無法反應,但要等一下
import tkinter as tk
from tkinter import filedialog, messagebox
import subprocess
import threading
def browse_python_file():
file_path = filedialog.askopenfilename(filetypes=[("Python Files", "*.py")])
python_file_entry.delete(0, tk.END)
python_file_entry.insert(0, file_path)
def browse_icon_file():
file_path = filedialog.askopenfilename(filetypes=[("Icon Files", "*.ico")])
icon_file_entry.delete(0, tk.END)
icon_file_entry.insert(0, file_path)
def browse_binary_file():
file_path = filedialog.askopenfilename(filetypes=[("All Files", "*.*")])
binary_file_entry.delete(0, tk.END)
binary_file_entry.insert(0, file_path)
def start_packaging():
python_file = python_file_entry.get()
icon_file = icon_file_entry.get()
binary_file = binary_file_entry.get()
windowed_mode = windowed_var.get()
no_console_mode = no_console_var.get()
if not python_file:
messagebox.showerror("錯誤", "請選擇 Python 檔案")
return
# 建立 PyInstaller 命令
command = ['pyinstaller', '--onefile', python_file]
if icon_file:
command += ['--icon', icon_file]
if binary_file:
command += ['--add-binary', binary_file]
if windowed_mode:
command.append('--windowed')
if no_console_mode:
command.append('--noconsole')
print(f"執行的命令: {' '.join(command)}")
# 使用線程執行打包過程
threading.Thread(target=run_command, args=(command,)).start()
def run_command(command):
try:
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
stdout, stderr = process.communicate()
if process.returncode == 0:
messagebox.showinfo("成功", f"打包完成!\n{stdout}")
else:
messagebox.showerror("錯誤", f"打包失敗!\n{stderr}")
except Exception as e:
messagebox.showerror("錯誤", f"發生錯誤!\n{str(e)}")
# 設置主窗口
root = tk.Tk()
root.title("PyInstaller 打包工具 by WCS 2024.10.07")
root.geometry("450x400")
# Python文件選擇
tk.Label(root, text="選擇 Python 檔案:").pack(pady=5)
python_file_entry = tk.Entry(root, width=50)
python_file_entry.pack(pady=5)
tk.Button(root, text="瀏覽", command=browse_python_file).pack()
# 圖標文件選擇
tk.Label(root, text="選擇圖標檔案 (可選):").pack(pady=5)
icon_file_entry = tk.Entry(root, width=50)
icon_file_entry.pack(pady=5)
tk.Button(root, text="瀏覽", command=browse_icon_file).pack()
# Binary文件選擇
tk.Label(root, text="選擇要添加的 Binary 檔案 (可選):").pack(pady=5)
binary_file_entry = tk.Entry(root, width=50)
binary_file_entry.pack(pady=5)
tk.Button(root, text="瀏覽", command=browse_binary_file).pack()
# Windowed 模式選項
windowed_var = tk.BooleanVar()
tk.Checkbutton(root, text="使用 Windowed 模式", variable=windowed_var).pack(pady=5)
# No Console 模式選項
no_console_var = tk.BooleanVar()
tk.Checkbutton(root, text="使用 No Console 模式", variable=no_console_var).pack(pady=5)
# 開始打包按鈕
tk.Button(root, text="開始打包", command=start_packaging).pack(pady=20)
root.mainloop()
隱藏命令行窗口: 如果你的應用是 GUI 應用程序,可以加上 --noconsole 參數來隱藏命令行窗口:
pyinstaller --onefile --noconsole your_script.py
自定義圖標: 你可以為生成的可執行檔指定圖標,使用 --icon 參數:
pyinstaller --onefile --icon=your_icon.ico your_script.py
指定輸出目錄: 如果想要指定可執行檔的輸出目錄,可以使用 --distpath 參數:
pyinstaller --onefile --distpath=output_dir your_script.py
使用規範文件: 如果有很多自定義選項,可以使用 .spec 文件來管理設定。生成 .spec 文件的命令是:
pyinstaller your_script.py --name=your_app_name --onefile
之後可以修改這個 .spec 文件,然後用以下命令打包:
pyinstaller your_spec_file.spec
包含額外的文件: 若需要將額外的文件(例如數據文件、配置文件等)包含在可執行檔中,可以使用 --add-data 參數:
pyinstaller --onefile --add-data "data.txt;." your_script.py
設置打包的壓縮選項: 你可以使用 --compress 參數來設置壓縮等級(0-9):
pyinstaller --onefile --compress=9 your_script.py
設置環境變量: 若需要在執行時設置環境變量,可以使用 --env 參數:
pyinstaller --onefile --env VAR_NAME=value your_script.py
自定義二進制文件名稱: 可以使用 --name 參數自定義生成的可執行檔名稱:
pyinstaller --onefile --name=my_custom_name your_script.py