Python Script compiles but won't work as .exe

70 Views Asked by At

I'm trying to a this script work as a standalone .exe application.I can run it from command line just fine and also compiling to .exe works without throwing any errors. But when I start the .exe only the GUI is visible, it has no function at all. I can drag files on the GUI as expected but it won't process any of them.

This my full script:

import tkinter as tk
from tkinterdnd2 import DND_FILES, TkinterDnD
from pathlib import Path
from PIL import Image
import os
import webbrowser

# Funktion zum Anzeigen des Texts
def show_text():
    info_label.config(text="Ziehe deine Bilder hier her", font=("Helvetica", 14, "bold"))

# Funktion zum Öffnen des Ausgabeordners
def open_output_folder():
    desktop = os.path.expanduser("~/Desktop")
    output_folder = os.path.join(desktop, "files")
    if os.path.exists(output_folder):
        webbrowser.open(output_folder)

def check_and_save_image(file_path):
    try:
        original_name = Path(file_path).stem

        if "-col" in original_name and not original_name.endswith("-col8"):
            error_label.config(text="Konvertierung verweigert!")
            return

        image = Image.open(file_path)

        aspect_ratio = image.width / image.height
        if abs(aspect_ratio - (4 / 3)) > 0.01:
            error_label.config(text="Falsches Seitenverhältnis!")
            return

        desktop = os.path.expanduser("~/Desktop")
        output_folder = os.path.join(desktop, "files")
        if not os.path.exists(output_folder):
            os.makedirs(output_folder)

        if "col8" in original_name:
            original_name = original_name.replace("-col8", "")

        for col, width in [("-col8", 780), ("-col7", 678), ("-col6", 576), ("-col5", 474), ("-col4-5", 423), ("-col4", 372), ("-col3", 270), ("-col2", 168)]:
            scaled_image = image.resize((width, int(width * 3 / 4)))

            file_name = f"{original_name}{col}.jpg"
            file_path_jpg = os.path.join(output_folder, file_name)
            scaled_image.save(file_path_jpg, "JPEG", quality=80)
            print(f"JPEG gespeichert: {file_name}")

            if col == "-col6":
                file_name_webp = os.path.join(output_folder, f"{original_name}{col}.webp")
                scaled_image.save(file_name_webp, "WebP", quality=50)
                print(f"WebP gespeichert: {file_name_webp}")

        success_label.config(text="Bilder erfolgreich gespeichert!")
        open_output_folder()
        root.after(2000, clear_labels)
    except Exception as e:
        error_label.config(text=str(e))

def clear_labels():
    error_label.config(text="")
    success_label.config(text="")
    show_text()

def drop(event):
    raw_file_paths = event.data
    file_paths = []
    temp_path = ""
    inside_braces = False
    for char in raw_file_paths:
        if char == '{':
            inside_braces = True
            temp_path += char
        elif char == '}':
            inside_braces = False
            temp_path += char
            file_paths.append(temp_path.strip('{}'))
            temp_path = ""
        elif inside_braces or char != ' ':
            temp_path += char

    for file_path in file_paths:
        check_and_save_image(file_path)

root = TkinterDnD.Tk()
root.title("Col Resizer 2.0")
root.iconbitmap("images/icon.ico")
root.geometry("640x480")

error_label = tk.Label(root, text="", fg="red")
error_label.pack()

success_label = tk.Label(root, text="", fg="green")
success_label.pack()

info_label = tk.Label(root, text="", font=("Helvetica", 14, "bold"))
info_label.pack()
show_text()

root.drop_target_register(DND_FILES)
root.dnd_bind('<<Drop>>', drop)

root.mainloop()

This is the code I use for pyinstaller. It works without any errors when compiling.

pyinstaller --noconfirm --onefile --windowed --icon "C:\entwicklung\dev-shop\projekte\resizer\icon.ico"  --additional-hooks-dir=.  --add-data 'C:\entwicklung\dev-shop\projekte\resizer'  "C:/entwicklung/dev-shop/projekte/Resizer/resizer.py"

Any ideas how to fix it? thank you in advance

0

There are 0 best solutions below