-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython2ExeConVerTer_GUI.py
More file actions
317 lines (255 loc) · 13.7 KB
/
Python2ExeConVerTer_GUI.py
File metadata and controls
317 lines (255 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import tkinter as tk
from tkinter import ttk, filedialog, messagebox, scrolledtext
import os
import subprocess
import threading
import sys
from pathlib import Path
class PythonToExeConverter:
def __init__(self, root):
self.root = root
self.root.title("Python to EXE Converter")
self.root.geometry("800x600")
self.root.minsize(700, 500)
# Variables
self.selected_files = []
self.output_dir = ""
# Configurar estilo
self.setup_style()
# Crear interfaz
self.create_widgets()
# Verificar PyInstaller
self.check_pyinstaller()
def setup_style(self):
"""Configurar estilo moderno"""
style = ttk.Style()
style.theme_use('clam')
# Colores modernos
style.configure('Title.TLabel', font=('Arial', 16, 'bold'), foreground='#2c3e50')
style.configure('Heading.TLabel', font=('Arial', 10, 'bold'), foreground='#34495e')
style.configure('Modern.TButton', padding=(10, 5))
def create_widgets(self):
"""Crear la interfaz gráfica"""
main_frame = ttk.Frame(self.root, padding="20")
main_frame.grid(row=0, column=0, sticky=(tk.W, tk.E, tk.N, tk.S))
# Configurar grid
self.root.columnconfigure(0, weight=1)
self.root.rowconfigure(0, weight=1)
main_frame.columnconfigure(1, weight=1)
# Título
title_label = ttk.Label(main_frame, text="Python to EXE Converter",
style='Title.TLabel')
title_label.grid(row=0, column=0, columnspan=3, pady=(0, 20))
# Sección de selección de archivos
files_frame = ttk.LabelFrame(main_frame, text="Selección de Archivos Python",
padding="10")
files_frame.grid(row=1, column=0, columnspan=3, sticky=(tk.W, tk.E), pady=(0, 10))
files_frame.columnconfigure(0, weight=1)
# Botones de selección rápida
quick_buttons_frame = ttk.Frame(files_frame)
quick_buttons_frame.grid(row=0, column=0, columnspan=3, sticky=(tk.W, tk.E), pady=(0, 10))
ttk.Button(quick_buttons_frame, text="Desktop",
command=lambda: self.browse_folder(Path.home() / "Desktop"),
style='Modern.TButton').pack(side=tk.LEFT, padx=(0, 5))
ttk.Button(quick_buttons_frame, text="Documents",
command=lambda: self.browse_folder(Path.home() / "Documents"),
style='Modern.TButton').pack(side=tk.LEFT, padx=5)
ttk.Button(quick_buttons_frame, text="Downloads",
command=lambda: self.browse_folder(Path.home() / "Downloads"),
style='Modern.TButton').pack(side=tk.LEFT, padx=5)
ttk.Button(quick_buttons_frame, text="Examinar...",
command=self.browse_files,
style='Modern.TButton').pack(side=tk.LEFT, padx=5)
# Lista de archivos seleccionados
ttk.Label(files_frame, text="Archivos seleccionados:",
style='Heading.TLabel').grid(row=1, column=0, sticky=tk.W, pady=(10, 5))
self.files_listbox = tk.Listbox(files_frame, height=6, selectmode=tk.EXTENDED)
self.files_listbox.grid(row=2, column=0, columnspan=2, sticky=(tk.W, tk.E), pady=(0, 10))
files_scrollbar = ttk.Scrollbar(files_frame, orient=tk.VERTICAL,
command=self.files_listbox.yview)
files_scrollbar.grid(row=2, column=2, sticky=(tk.N, tk.S), pady=(0, 10))
self.files_listbox.config(yscrollcommand=files_scrollbar.set)
# Botón para eliminar archivos seleccionados
ttk.Button(files_frame, text="Eliminar seleccionados",
command=self.remove_selected_files).grid(row=3, column=0, sticky=tk.W)
# Sección de configuración de salida
output_frame = ttk.LabelFrame(main_frame, text="Configuración de Salida",
padding="10")
output_frame.grid(row=2, column=0, columnspan=3, sticky=(tk.W, tk.E), pady=(0, 10))
output_frame.columnconfigure(1, weight=1)
ttk.Label(output_frame, text="Carpeta de salida:").grid(row=0, column=0, sticky=tk.W, padx=(0, 10))
self.output_var = tk.StringVar()
self.output_var.set(str(Path.home() / "Desktop"))
self.output_dir = str(Path.home() / "Desktop")
output_entry = ttk.Entry(output_frame, textvariable=self.output_var, state='readonly')
output_entry.grid(row=0, column=1, sticky=(tk.W, tk.E), padx=(0, 10))
ttk.Button(output_frame, text="Examinar",
command=self.browse_output_folder).grid(row=0, column=2)
# Opciones adicionales
options_frame = ttk.LabelFrame(main_frame, text="Opciones", padding="10")
options_frame.grid(row=3, column=0, columnspan=3, sticky=(tk.W, tk.E), pady=(0, 10))
self.onefile_var = tk.BooleanVar(value=True)
ttk.Checkbutton(options_frame, text="Un solo archivo ejecutable (--onefile)",
variable=self.onefile_var).grid(row=0, column=0, sticky=tk.W)
self.noconsole_var = tk.BooleanVar()
ttk.Checkbutton(options_frame, text="Sin ventana de consola (--noconsole)",
variable=self.noconsole_var).grid(row=1, column=0, sticky=tk.W)
# Botón de conversión
convert_frame = ttk.Frame(main_frame)
convert_frame.grid(row=4, column=0, columnspan=3, pady=20)
self.convert_button = ttk.Button(convert_frame, text="Convertir a EXE",
command=self.start_conversion,
style='Modern.TButton')
self.convert_button.pack(pady=10)
# Barra de progreso
self.progress = ttk.Progressbar(main_frame, mode='indeterminate')
self.progress.grid(row=5, column=0, columnspan=3, sticky=(tk.W, tk.E), pady=(0, 10))
# Log de salida
log_frame = ttk.LabelFrame(main_frame, text="Log de Conversión", padding="10")
log_frame.grid(row=6, column=0, columnspan=3, sticky=(tk.W, tk.E, tk.N, tk.S), pady=(0, 10))
log_frame.columnconfigure(0, weight=1)
log_frame.rowconfigure(0, weight=1)
main_frame.rowconfigure(6, weight=1)
self.log_text = scrolledtext.ScrolledText(log_frame, height=8, state='disabled')
self.log_text.grid(row=0, column=0, sticky=(tk.W, tk.E, tk.N, tk.S))
def check_pyinstaller(self):
"""Verificar si PyInstaller está instalado"""
try:
subprocess.run([sys.executable, '-c', 'import PyInstaller'],
check=True, capture_output=True)
self.log_message("✓ PyInstaller detectado correctamente")
except (subprocess.CalledProcessError, FileNotFoundError):
self.log_message("⚠ PyInstaller no está instalado")
response = messagebox.askyesno(
"PyInstaller requerido",
"PyInstaller no está instalado. ¿Desea instalarlo automáticamente?\n"
"Esto puede tardar unos minutos."
)
if response:
self.install_pyinstaller()
def install_pyinstaller(self):
"""Instalar PyInstaller automáticamente"""
def install():
try:
self.log_message("Instalando PyInstaller...")
self.progress.start()
result = subprocess.run([sys.executable, '-m', 'pip', 'install', 'pyinstaller'],
capture_output=True, text=True)
self.progress.stop()
if result.returncode == 0:
self.log_message("✓ PyInstaller instalado correctamente")
else:
self.log_message(f"✗ Error instalando PyInstaller: {result.stderr}")
except Exception as e:
self.progress.stop()
self.log_message(f"✗ Error instalando PyInstaller: {str(e)}")
threading.Thread(target=install, daemon=True).start()
def browse_files(self):
"""Examinar archivos Python"""
files = filedialog.askopenfilenames(
title="Seleccionar archivos Python",
filetypes=[("Python files", "*.py"), ("All files", "*.*")]
)
if files:
for file in files:
if file not in self.selected_files:
self.selected_files.append(file)
self.files_listbox.insert(tk.END, os.path.basename(file))
def browse_folder(self, default_path=None):
"""Examinar carpeta para buscar archivos Python"""
if default_path and default_path.exists():
initialdir = str(default_path)
else:
initialdir = str(Path.home())
folder = filedialog.askdirectory(title="Seleccionar carpeta", initialdir=initialdir)
if folder:
py_files = list(Path(folder).glob("*.py"))
added_count = 0
for file in py_files:
file_str = str(file)
if file_str not in self.selected_files:
self.selected_files.append(file_str)
self.files_listbox.insert(tk.END, file.name)
added_count += 1
if added_count > 0:
self.log_message(f"✓ Se agregaron {added_count} archivos Python de {folder}")
else:
self.log_message(f"No se encontraron archivos Python nuevos en {folder}")
def remove_selected_files(self):
"""Eliminar archivos seleccionados de la lista"""
selected_indices = self.files_listbox.curselection()
for index in reversed(selected_indices):
del self.selected_files[index]
self.files_listbox.delete(index)
def browse_output_folder(self):
"""Examinar carpeta de salida"""
folder = filedialog.askdirectory(title="Seleccionar carpeta de salida")
if folder:
self.output_dir = folder
self.output_var.set(folder)
def log_message(self, message):
"""Agregar mensaje al log"""
self.log_text.config(state='normal')
self.log_text.insert(tk.END, f"{message}\n")
self.log_text.see(tk.END)
self.log_text.config(state='disabled')
self.root.update_idletasks()
def start_conversion(self):
"""Iniciar proceso de conversión en hilo separado"""
if not self.selected_files:
messagebox.showwarning("Sin archivos", "Por favor selecciona al menos un archivo Python.")
return
self.convert_button.config(state='disabled')
self.progress.start()
thread = threading.Thread(target=self.convert_files, daemon=True)
thread.start()
def convert_files(self):
"""Convertir archivos Python a EXE"""
successful_conversions = 0
total_files = len(self.selected_files)
self.log_message(f"Iniciando conversión de {total_files} archivo(s)...")
for i, file_path in enumerate(self.selected_files, 1):
try:
self.log_message(f"[{i}/{total_files}] Procesando: {os.path.basename(file_path)}")
# Construir comando PyInstaller
cmd = [sys.executable, '-m', 'PyInstaller']
if self.onefile_var.get():
cmd.append('--onefile')
if self.noconsole_var.get():
cmd.append('--noconsole')
cmd.extend(['--distpath', self.output_dir])
cmd.extend(['--workpath', os.path.join(self.output_dir, 'build')])
cmd.extend(['--specpath', os.path.join(self.output_dir, 'spec')])
cmd.append('--clean')
cmd.append(file_path)
# Ejecutar PyInstaller
result = subprocess.run(cmd, capture_output=True, text=True,
cwd=os.path.dirname(file_path))
if result.returncode == 0:
successful_conversions += 1
self.log_message(f"✓ {os.path.basename(file_path)} convertido exitosamente")
else:
self.log_message(f"✗ Error convirtiendo {os.path.basename(file_path)}")
if result.stderr:
self.log_message(f"Error: {result.stderr[:200]}...")
except Exception as e:
self.log_message(f"✗ Excepción procesando {os.path.basename(file_path)}: {str(e)}")
# Finalizar
self.progress.stop()
self.convert_button.config(state='normal')
self.log_message(f"\n=== Conversión completada ===")
self.log_message(f"Exitosos: {successful_conversions}/{total_files}")
self.log_message(f"Archivos EXE guardados en: {self.output_dir}")
if successful_conversions > 0:
messagebox.showinfo("Conversión completada",
f"Se convirtieron {successful_conversions} de {total_files} archivos exitosamente.\n"
f"Los archivos EXE están en: {self.output_dir}")
else:
messagebox.showerror("Conversión fallida",
"No se pudo convertir ningún archivo. Revisa el log para más detalles.")
def main():
root = tk.Tk()
app = PythonToExeConverter(root)
root.mainloop()
if __name__ == "__main__":
main()