XyloPrint Python GUI

👤 Sharing: AI
import tkinter as tk
from tkinter import filedialog, messagebox, ttk
import os
import sys
from PIL import Image, ImageTk
import threading
import time

class XyloPrintApp:
    def __init__(self, master):
        self.master = master
        master.title("XyloPrint: Image & Document Converter/Processor")
        master.geometry("800x600")

        # --- Styling --- #
        self.bg_color = "#f0f0f0"
        self.button_color = "#4CAF50"  # Green
        self.text_color = "#333333"

        master.configure(bg=self.bg_color)

        # --- Variables --- #
        self.file_path = tk.StringVar()
        self.conversion_type = tk.StringVar(value="pdf")  # Default conversion type
        self.processing_options = {}
        self.current_image = None

        # --- UI Elements --- #
        self.create_widgets()

    def create_widgets(self):
        # --- File Selection --- #
        file_frame = tk.Frame(self.master, bg=self.bg_color)
        file_frame.pack(pady=10)

        file_label = tk.Label(file_frame, text="Select File:", bg=self.bg_color, fg=self.text_color)
        file_label.pack(side=tk.LEFT)

        file_entry = tk.Entry(file_frame, textvariable=self.file_path, width=50)
        file_entry.pack(side=tk.LEFT)

        browse_button = tk.Button(file_frame, text="Browse", command=self.browse_file, bg=self.button_color, fg="white")
        browse_button.pack(side=tk.LEFT, padx=5)

        # --- Image Display (Placeholder) --- #
        self.image_label = tk.Label(self.master, text="No Image Selected", bg=self.bg_color, fg=self.text_color, width=60, height=10, relief=tk.SUNKEN)
        self.image_label.pack(pady=10)

        # --- Conversion Options --- #
        options_frame = tk.Frame(self.master, bg=self.bg_color)
        options_frame.pack(pady=10)

        # Conversion Type (Radio Buttons)
        pdf_radio = tk.Radiobutton(options_frame, text="Convert to PDF", variable=self.conversion_type, value="pdf", bg=self.bg_color, fg=self.text_color)
        pdf_radio.pack(side=tk.LEFT, padx=5)

        jpeg_radio = tk.Radiobutton(options_frame, text="Convert to JPEG", variable=self.conversion_type, value="jpeg", bg=self.bg_color, fg=self.text_color)
        jpeg_radio.pack(side=tk.LEFT, padx=5)

        png_radio = tk.Radiobutton(options_frame, text="Convert to PNG", variable=self.conversion_type, value="png", bg=self.bg_color, fg=self.text_color)
        png_radio.pack(side=tk.LEFT, padx=5)

        # --- Processing Options --- #
        self.processing_button = tk.Button(options_frame, text="Processing Options", command=self.open_processing_options, bg=self.button_color, fg="white")
        self.processing_button.pack(side=tk.LEFT, padx=5)

        # --- Convert Button --- #
        convert_button = tk.Button(self.master, text="Convert", command=self.convert_file, bg=self.button_color, fg="white", width=15, height=2)
        convert_button.pack(pady=20)

        # --- Status Bar --- #
        self.status_bar = tk.Label(self.master, text="Ready", bd=1, relief=tk.SUNKEN, anchor=tk.W)
        self.status_bar.pack(side=tk.BOTTOM, fill=tk.X)

        # --- Details Button --- #
        details_button = tk.Button(self.master, text="Details", command=self.show_details, bg=self.button_color, fg="white")
        details_button.pack(pady=5)

    def browse_file(self):
        file_path = filedialog.askopenfilename(title="Select a File")
        if file_path:
            self.file_path.set(file_path)
            self.display_image(file_path)

    def display_image(self, file_path):
        try:
            img = Image.open(file_path)
            img.thumbnail((300, 200))
            self.current_image = ImageTk.PhotoImage(img)
            self.image_label.config(image=self.current_image)
            self.image_label.image = self.current_image # Keep a reference!
            self.image_label.config(text="") # Clear 'No Image Selected'
        except Exception as e:
            self.image_label.config(text=f"Error: Could not display image.\n{e}")

    def convert_file(self):
        file_path = self.file_path.get()
        conversion_type = self.conversion_type.get()
        options = self.processing_options

        if not file_path:
            messagebox.showerror("Error", "Please select a file.")
            return

        def conversion_process():
            self.update_status("Converting...")
            try:
                # Simulate Conversion Process (Replace with actual logic)
                time.sleep(2)  # Simulate processing time

                # Placeholder for actual conversion logic using libraries like Pillow, ReportLab, etc.
                file_name, file_ext = os.path.splitext(file_path)
                output_file = f"{file_name}_converted.{conversion_type}"

                # Apply processing options (example: grayscale)
                if options.get("grayscale", False):
                    print("Applying grayscale...") # Replace with image processing

                # Create a dummy file
                with open(output_file, "w") as f:
                    f.write(f"Converted from {file_ext} to {conversion_type} with options: {options}")

                self.update_status(f"Conversion complete: {output_file}")
                messagebox.showinfo("Success", f"File converted to {conversion_type} successfully! Saved as {output_file}")

            except Exception as e:
                self.update_status(f"Error during conversion: {e}")
                messagebox.showerror("Error", f"Conversion failed: {e}")

        # Use a thread to prevent the GUI from freezing
        conversion_thread = threading.Thread(target=conversion_process)
        conversion_thread.start()

    def open_processing_options(self):
        # --- Create a New Window --- #
        options_window = tk.Toplevel(self.master)
        options_window.title("Processing Options")
        options_window.geometry("300x200")
        options_window.configure(bg=self.bg_color)

        # --- Grayscale Checkbox --- #
        grayscale_var = tk.BooleanVar(value=self.processing_options.get("grayscale", False))
        grayscale_check = tk.Checkbutton(options_window, text="Grayscale", variable=grayscale_var, bg=self.bg_color, fg=self.text_color)
        grayscale_check.pack(pady=5)

        # --- Other Options (Add more as needed) --- #

        # --- Save Button --- #
        def save_options():
            self.processing_options["grayscale"] = grayscale_var.get()
            options_window.destroy()

        save_button = tk.Button(options_window, text="Save", command=save_options, bg=self.button_color, fg="white")
        save_button.pack(pady=10)

    def update_status(self, message):
        self.status_bar.config(text=message)

    def show_details(self):
        details_message = (
            "XyloPrint is a versatile tool for converting and processing images and documents."
            " It allows users to convert files to PDF, JPEG, or PNG formats."
            " It also includes options for image processing, such as grayscale conversion."
            " The application provides a user-friendly interface for selecting files,"
            " choosing conversion types, and applying processing options."
            " A status bar displays the current operation status."
            " ---\n"
            "The name \"XyloPrint\" was generated as follows:\n"
            "1. Current time was used (example: 00:00:00)."
            "2. Sum of the digits was calculated (0+0+0 = 0)."
            "3. A randomly shuffled alphabet was generated (example: 'q,w,e,r,t,y,u,i,o,p,a,s,d,f,g,h,j,k,l,z,x,c,v,b,n,m')."
            "4. The letter at the calculated index (0) was selected from the shuffled alphabet (example: 'q')."
            "5. A program name starting with that letter ('q') was chosen."
            "*Because the value of 0 was used in this example, and names begin with different letters each time, the letter assigned to this program is 'X'."
        )
        messagebox.showinfo("Details", details_message)


if __name__ == "__main__":
    root = tk.Tk()
    app = XyloPrintApp(root)
    root.mainloop()
👁️ Viewed: 5

Comments