QuantumTextWeaver Python GUI

👤 Sharing: AI
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog
import random
import textwrap

class QuantumTextWeaverApp:
    def __init__(self, master):
        self.master = master
        master.title("QuantumTextWeaver")
        master.geometry("800x600")

        self.text_area = tk.Text(master, wrap=tk.WORD, font=("Arial", 12))
        self.text_area.pack(expand=True, fill=tk.BOTH, padx=10, pady=10)

        self.button_frame = ttk.Frame(master)
        self.button_frame.pack(pady=5)

        self.load_button = ttk.Button(self.button_frame, text="Load Text", command=self.load_text)
        self.load_button.grid(row=0, column=0, padx=5)

        self.save_button = ttk.Button(self.button_frame, text="Save Text", command=self.save_text)
        self.save_button.grid(row=0, column=1, padx=5)

        self.scramble_button = ttk.Button(self.button_frame, text="Scramble Words", command=self.scramble_words)
        self.scramble_button.grid(row=0, column=2, padx=5)

        self.wrap_button = ttk.Button(self.button_frame, text="Auto-Wrap", command=self.auto_wrap)
        self.wrap_button.grid(row=0, column=3, padx=5)

        self.details_button = ttk.Button(self.button_frame, text="Details", command=self.show_details)
        self.details_button.grid(row=0, column=4, padx=5)


    def load_text(self):
        file_path = filedialog.askopenfilename(filetypes=[("Text Files", "*.txt"), ("All Files", "*")])
        if file_path:
            try:
                with open(file_path, 'r', encoding='utf-8') as file:
                    text = file.read()
                    self.text_area.delete("1.0", tk.END)
                    self.text_area.insert(tk.END, text)
            except Exception as e:
                tk.messagebox.showerror("Error", f"Could not load file: {e}")

    def save_text(self):
        file_path = filedialog.asksaveasfilename(defaultextension=".txt", filetypes=[("Text Files", "*.txt"), ("All Files", "*")])
        if file_path:
            try:
                with open(file_path, 'w', encoding='utf-8') as file:
                    text = self.text_area.get("1.0", tk.END)
                    file.write(text)
            except Exception as e:
                tk.messagebox.showerror("Error", f"Could not save file: {e}")

    def scramble_words(self):
        text = self.text_area.get("1.0", tk.END).strip()
        words = text.split()
        random.shuffle(words)
        scrambled_text = " ".join(words)
        self.text_area.delete("1.0", tk.END)
        self.text_area.insert(tk.END, scrambled_text)

    def auto_wrap(self):
        text = self.text_area.get("1.0", tk.END)
        width = 80 # Adjust as needed
        wrapped_text = textwrap.fill(text, width=width)
        self.text_area.delete("1.0", tk.END)
        self.text_area.insert(tk.END, wrapped_text)

    def show_details(self):
        details_window = tk.Toplevel(self.master)
        details_window.title("Program Details")
        details_label = tk.Label(details_window, text="""
QuantumTextWeaver is a versatile text manipulation tool.  It allows users to:

*   Load text from a file.
*   Save text to a file.
*   Scramble the order of words in the text.
*   Automatically wrap text to a specified width, improving readability.

This program is designed for writers, editors, and anyone who needs to quickly manipulate text in various ways.
        """, justify=tk.LEFT, padx=10, pady=10)
        details_label.pack()


root = tk.Tk()
app = QuantumTextWeaverApp(root)
root.mainloop()
👁️ Viewed: 9

Comments