Zephyrian Automata Python GUI

👤 Sharing: AI
import tkinter as tk
from tkinter import ttk, filedialog, messagebox
import os
import datetime
import random

class ZephyrianAutomata:
    def __init__(self, master):
        self.master = master
        master.title("Zephyrian Automata: Versatile File Processor")
        master.geometry("800x600")

        self.style = ttk.Style()
        self.style.configure('TButton', padding=5, relief="raised")
        self.style.configure('TLabel', padding=5)
        self.style.configure('TEntry', padding=5)

        # --- File Selection Frame ---
        self.file_frame = ttk.Frame(master, padding=10)
        self.file_frame.pack(fill=tk.X)

        self.file_label = ttk.Label(self.file_frame, text="Select File:")
        self.file_label.pack(side=tk.LEFT)

        self.file_path = tk.StringVar()
        self.file_entry = ttk.Entry(self.file_frame, textvariable=self.file_path, width=60)
        self.file_entry.pack(side=tk.LEFT, expand=True, fill=tk.X)

        self.browse_button = ttk.Button(self.file_frame, text="Browse", command=self.browse_file)
        self.browse_button.pack(side=tk.LEFT)

        # --- Functionality Selection Frame ---
        self.function_frame = ttk.Frame(master, padding=10)
        self.function_frame.pack(fill=tk.X)

        self.function_label = ttk.Label(self.function_frame, text="Select Function:")
        self.function_label.pack(side=tk.LEFT)

        self.function_choices = ["Count Words", "Count Lines", "Find and Replace", "Extract Email Addresses", "Sort Lines"]
        self.function_combo = ttk.Combobox(self.function_frame, values=self.function_choices, state="readonly")
        self.function_combo.pack(side=tk.LEFT, expand=True, fill=tk.X)
        self.function_combo.set(self.function_choices[0]) # Default value

        # --- Operation Frame (Specific parameters for operations) --- 
        self.operation_frame = ttk.Frame(master, padding=10)
        self.operation_frame.pack(fill=tk.X)

        self.param_label = ttk.Label(self.operation_frame, text="Parameter:")
        self.param_label.pack(side=tk.LEFT)

        self.param_entry = ttk.Entry(self.operation_frame, width=40)
        self.param_entry.pack(side=tk.LEFT, expand=True, fill=tk.X)

        # --- Output Text Area --- 
        self.output_frame = ttk.Frame(master, padding=10)
        self.output_frame.pack(fill=tk.BOTH, expand=True)

        self.output_text = tk.Text(self.output_frame, wrap=tk.WORD, height=15)
        self.output_text.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)

        self.output_scroll = ttk.Scrollbar(self.output_frame, command=self.output_text.yview)
        self.output_scroll.pack(side=tk.RIGHT, fill=tk.Y)
        self.output_text['yscrollcommand'] = self.output_scroll.set

        # --- Buttons --- 
        self.button_frame = ttk.Frame(master, padding=10)
        self.button_frame.pack()

        self.run_button = ttk.Button(self.button_frame, text="Run", command=self.run_function)
        self.run_button.pack(side=tk.LEFT, padx=5)

        self.clear_button = ttk.Button(self.button_frame, text="Clear Output", command=self.clear_output)
        self.clear_button.pack(side=tk.LEFT, padx=5)

        self.details_button = ttk.Button(self.button_frame, text="Details", command=self.show_details)
        self.details_button.pack(side=tk.LEFT, padx=5)

    def browse_file(self):
        filename = filedialog.askopenfilename()
        self.file_path.set(filename)

    def run_function(self):
        filepath = self.file_path.get()
        function_name = self.function_combo.get()
        parameter = self.param_entry.get()

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

        try:
            with open(filepath, 'r', encoding='utf-8') as f:
                file_content = f.read()
        except FileNotFoundError:
            messagebox.showerror("Error", "File not found.")
            return
        except Exception as e:
            messagebox.showerror("Error", f"Error reading file: {e}")
            return

        if function_name == "Count Words":
            result = len(file_content.split())
            self.output_text.insert(tk.END, f"Word Count: {result}\n")
        elif function_name == "Count Lines":
            result = file_content.count('\n') + 1
            self.output_text.insert(tk.END, f"Line Count: {result}\n")
        elif function_name == "Find and Replace":
            if not parameter:
                messagebox.showerror("Error", "Please enter a 'find and replace' parameter (e.g., old,new).")
                return
            try:
                old_text, new_text = parameter.split(',')
                result = file_content.replace(old_text, new_text)
                self.output_text.insert(tk.END, f"Find and Replace Result:\n{result}\n")
            except ValueError:
                messagebox.showerror("Error", "Invalid 'find and replace' parameter format. Use 'old,new'.")
                return
        elif function_name == "Extract Email Addresses":
            import re
            emails = re.findall(r'[\w\.-]+@[\w\.-]+', file_content)
            self.output_text.insert(tk.END, f"Extracted Email Addresses:\n{chr(10).join(emails)}\n")
        elif function_name == "Sort Lines":
            lines = file_content.splitlines()
            lines.sort()
            result = '\n'.join(lines)
            self.output_text.insert(tk.END, f"Sorted Lines:\n{result}\n")

    def clear_output(self):
        self.output_text.delete('1.0', tk.END)

    def show_details(self):
        details_message = (
            "Zephyrian Automata is a versatile file processing tool designed to perform a variety of text-based operations.  " 
            "It provides a user-friendly interface for selecting files and applying different functions, making it a useful "
            "alternative to more complex, paid software.  \n\n" 
            "Available functions include:\n" 
            "- **Count Words:**  Counts the number of words in the selected file.\n" 
            "- **Count Lines:** Counts the number of lines in the selected file.\n" 
            "- **Find and Replace:** Replaces occurrences of a specified text with another text.\n" 
            "- **Extract Email Addresses:** Extracts all email addresses found in the file.\n" 
            "- **Sort Lines:** Sorts the lines of the file alphabetically.\n\n" 
            "This tool aims to provide quick and efficient solutions for common text processing tasks."
        )
        messagebox.showinfo("Details", details_message)

# --- Main Application --- 
if __name__ == "__main__":
    root = tk.Tk()
    app = ZephyrianAutomata(root)
    root.mainloop()
👁️ Viewed: 5

Comments