def add_files(self): """添加单个或多个文件""" files = filedialog.askopenfilenames(filetypes=[("Python文件", "*.py")])
for f in files:
if f not in self.listbox.get(0, tk.END):
self.listbox.insert(tk.END, f)
def add_directory(self): """添加目录下的所有Python文件""" directory = filedialog.askdirectory()
if directory:
for root, _, files in os.walk(directory):
for file in files:
if file.endswith(".py"):
path = os.path.join(root, file)
if path not in self.listbox.get(0, tk.END):
self.listbox.insert(tk.END, path)
def choose_output(self): """选择输出目录""" path = filedialog.askdirectory()
if path:
self.output_path.set(path)
def choose_icon(self): """选择图标文件""" file = filedialog.askopenfilename(filetypes=[("图标文件", "*.ico")])
if file:
self.icon_path.set(file)
def start_conversion(self): """开始打包""" if self.listbox.size() == 0:
self.log("错误:请先添加要打包的文件!")
return
# 获取打包参数
output_dir = self.output_path.get() or os.getcwd()
options = []
if self.onefile.get(): options.append("--onefile")
if self.noconsole.get(): options.append("--windowed")
if self.icon_path.get(): options.extend(["--icon", self.icon_path.get()])
def run_pyinstaller(self, output_dir, options): """执行打包操作""" try:
for file in self.listbox.get(0, tk.END):
if not self.running: break
self.update_status(f"正在打包: {os.path.basename(file)}")
# 运行并捕获输出
with subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
encoding='utf-8', # 强制使用UTF-8编码
errors='replace', # 替换无法解码的字符
text=True,
bufsize=1
) as proc:
for line in proc.stdout:
self.log(line.strip())