好友
阅读权限 10
听众
最后登录 1970-1-1
本帖最后由 eason78 于 2024-11-26 17:07 编辑
有一个问题,就是在代码状态下运行打包成功后的exe是没问题的,
若它将打包成exe后再对py打包,就不行了。
来大佬解决。
Traceback (most recent call last):
File "PyInstaller\hooks\rthooks\pyi_rth__tkinter.py", line 36, in <module>
File "PyInstaller\hooks\rthooks\pyi_rth__tkinter.py", line 33, in _pyi_rthook
FileNotFoundError: Tk data directory "C:\Users\ADMINI~1\AppData\Local\Temp\_MEI93322\_tk_data" not found.
[Python] 纯文本查看 复制代码
import os
import sys
import threading
import subprocess
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog
def select_py_file():
file_path = filedialog.askopenfilename(filetypes=[("Python files", "*.py")])
if file_path:
e1.delete(0, tk.END)
e1.insert(0, file_path)
def select_ico_file():
file_path = filedialog.askopenfilename(filetypes=[("ICO files", "*.ico")])
if file_path:
e2.delete(0, tk.END)
e2.insert(0, file_path)
def in1():
"""处理添加ico图标选项的选择逻辑"""
global ico_path, use_ico
if radio1.get() == 1:
ico_path = filedialog.askopenfilename(filetypes=[("ICO files", "*.ico")])
if ico_path:
use_ico = True
else:
use_ico = False
else:
use_ico = False
def in3():
"""处理是否显示控制台选项的选择逻辑"""
global no_console
no_console = radio3.get() == 2
def start_packaging():
script_path = e1.get()
if not os.path.isfile(script_path):
tk.messagebox.showerror("错误", "Python脚本文件不存在,请重新选择")
return
if use_ico and not os.path.isfile(ico_path):
tk.messagebox.showerror("错误", "ICO图标文件不存在,请重新选择")
return
cmd = ['pyinstaller', '--onefile']
if no_console:
cmd.append('--windowed')
if use_ico:
cmd.extend(['--icon', ico_path])
cmd.append(script_path)
print("开始打包,请稍候...")
try:
subprocess.run(cmd, check=True)
tk.messagebox.showinfo("成功", "py打包成功!")
except subprocess.CalledProcessError as e:
tk.messagebox.showerror("失败", f"py打包失败:{e}")
except Exception as e:
tk.messagebox.showerror("错误", f"py发生错误:{e}")
def start_packaging_thread():
# 创建一个新的线程来执行打包操作
packaging_thread = threading.Thread(target=start_packaging)
packaging_thread.start()
# 创建主窗口
window = tk.Tk()
window.title("Py打包助手")
style = ttk.Style()
style.configure('TFrame')
style.configure('TLabel', font=('Arial', 10), padding=5)
style.configure('TRadiobutton', font=('Arial', 10), padding=5)
style.configure('TButton', font=('Arial', 10), padding=5)
# 选择Python脚本文件部分
py_frame = ttk.Frame(window, relief=tk.RAISED, borderwidth=2)
py_frame.pack(pady=10, padx=10, fill='x')
l1 = ttk.Label(py_frame, text="py文件")
l1.pack(side=tk.LEFT, padx=(0, 10))
e1 = ttk.Entry(py_frame, width=45)
e1.pack(side=tk.LEFT, fill='x', expand=True)
select_py_button = ttk.Button(py_frame, text="选择", command=select_py_file)
select_py_button.pack(side=tk.LEFT)
# 选择ICO图标文件部分(可选)
options_frame = ttk.Frame(window)
options_frame.pack(pady=5, padx=10, fill='x')
l2 = ttk.Label(options_frame, text="添加ico")
l2.pack(side=tk.LEFT)
radio1 = tk.IntVar()
R1 = ttk.Radiobutton(options_frame, text="是", variable=radio1, value=1, command=in1)
R1.pack(side=tk.LEFT)
R2 = ttk.Radiobutton(options_frame, text="否", variable=radio1, value=2, command=in1)
R2.pack(side=tk.LEFT)
l4 = ttk.Label(options_frame, text="显示控制台")
l4.pack(side=tk.LEFT)
radio3 = tk.IntVar()
R1 = ttk.Radiobutton(options_frame, text="是", variable=radio3, value=1, command=in3)
R1.pack(side=tk.LEFT)
R2 = ttk.Radiobutton(options_frame, text="否", variable=radio3, value=2, command=in3)
R2.pack(side=tk.LEFT)
# 按钮部分,放在同一行显示
button_frame = ttk.Frame(window)
button_frame.pack(pady=10)
button = ttk.Button(button_frame, text="开始打包", command=start_packaging_thread)
button.pack(side=tk.LEFT, padx=5)
# 初始化一些全局变量,用于记录相关选项状态
ico_path = None
use_ico = False
no_console = False
window.mainloop()