Uqvylwv Python GUI

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

class AdvancedTextEditor:
    def __init__(self, root):
        self.root = root
        self.root.title("Advanced Text Editor")
        self.root.geometry("800x600")

        self.text_area = tk.Text(self.root, wrap=tk.WORD, undo=True)
        self.text_area.pack(expand=True, fill=tk.BOTH)

        self.create_menu()
        self.create_toolbar()
        self.current_file = None

    def create_menu(self):
        menubar = tk.Menu(self.root)

        file_menu = tk.Menu(menubar, tearoff=0)
        file_menu.add_command(label="New", command=self.new_file)
        file_menu.add_command(label="Open", command=self.open_file)
        file_menu.add_command(label="Save", command=self.save_file)
        file_menu.add_command(label="Save As...", command=self.save_file_as)
        file_menu.add_separator()
        file_menu.add_command(label="Exit", command=self.root.quit)

        edit_menu = tk.Menu(menubar, tearoff=0)
        edit_menu.add_command(label="Undo", command=self.text_area.edit_undo)
        edit_menu.add_command(label="Redo", command=self.text_area.edit_redo)
        edit_menu.add_separator()
        edit_menu.add_command(label="Cut", command=lambda: self.text_area.event_generate("<<Cut>>"))
        edit_menu.add_command(label="Copy", command=lambda: self.text_area.event_generate("<<Copy>>"))
        edit_menu.add_command(label="Paste", command=lambda: self.text_area.event_generate("<<Paste>>"))
        edit_menu.add_command(label="Select All", command=lambda: self.text_area.tag_add(tk.SEL, "1.0", tk.END))

        view_menu = tk.Menu(menubar, tearoff=0)
        view_menu.add_command(label="Toggle Word Wrap", command=self.toggle_word_wrap)
        view_menu.add_command(label="Show Details", command=self.show_details)


        menubar.add_cascade(label="File", menu=file_menu)
        menubar.add_cascade(label="Edit", menu=edit_menu)
        menubar.add_cascade(label="View", menu=view_menu)

        self.root.config(menu=menubar)

    def create_toolbar(self):
        toolbar = ttk.Frame(self.root)
        toolbar.pack(side=tk.TOP, fill=tk.X)

        # Example toolbar buttons (you can add more)
        new_button = ttk.Button(toolbar, text="New", command=self.new_file)
        new_button.pack(side=tk.LEFT, padx=2, pady=2)

        open_button = ttk.Button(toolbar, text="Open", command=self.open_file)
        open_button.pack(side=tk.LEFT, padx=2, pady=2)

        save_button = ttk.Button(toolbar, text="Save", command=self.save_file)
        save_button.pack(side=tk.LEFT, padx=2, pady=2)

        # Add more buttons here as needed (e.g., bold, italic, etc.)


    def new_file(self):
        self.text_area.delete("1.0", tk.END)
        self.current_file = None
        self.root.title("Advanced Text Editor")

    def open_file(self):
        file_path = filedialog.askopenfilename(defaultextension=".txt",
                                            filetypes=[("All Files", "*.*"),
                                                       ("Text Documents", "*.txt")])
        if file_path:
            try:
                with open(file_path, "r") as file:
                    content = file.read()
                    self.text_area.delete("1.0", tk.END)
                    self.text_area.insert("1.0", content)
                    self.current_file = file_path
                    self.root.title(os.path.basename(file_path) + " - Advanced Text Editor")
            except Exception as e:
                messagebox.showerror("Error", f"Could not open file: {e}")

    def save_file(self):
        if self.current_file:
            try:
                with open(self.current_file, "w") as file:
                    content = self.text_area.get("1.0", tk.END)
                    file.write(content)
                messagebox.showinfo("Save", "File saved successfully!")
            except Exception as e:
                messagebox.showerror("Error", f"Could not save file: {e}")
        else:
            self.save_file_as()

    def save_file_as(self):
        file_path = filedialog.asksaveasfilename(defaultextension=".txt",
                                               filetypes=[("All Files", "*.*"),
                                                          ("Text Documents", "*.txt")])
        if file_path:
            try:
                with open(file_path, "w") as file:
                    content = self.text_area.get("1.0", tk.END)
                    file.write(content)
                self.current_file = file_path
                self.root.title(os.path.basename(file_path) + " - Advanced Text Editor")
                messagebox.showinfo("Save", "File saved successfully!")
            except Exception as e:
                messagebox.showerror("Error", f"Could not save file: {e}")

    def toggle_word_wrap(self):
        if self.text_area["wrap"] == tk.WORD:
            self.text_area["wrap"] = tk.NONE
        else:
            self.text_area["wrap"] = tk.WORD

    def show_details(self):
        messagebox.showinfo("Details", "This advanced text editor offers standard file operations (New, Open, Save, Save As) along with editing features like Undo, Redo, Cut, Copy, Paste, and Select All. It also supports toggling word wrap.  Additional features can be added, such as syntax highlighting, find/replace, and support for different file encodings, to enhance its functionality.")


if __name__ == "__main__":
    root = tk.Tk()

    # Naming Logic
    import datetime

    now = datetime.datetime.now()
    hour = now.hour
    minute = now.minute
    second = now.second

    sum_digits = (hour + minute + second) % 26

    alphabet = list('abcdefghijklmnopqrstuvwxyz')
    random.shuffle(alphabet)

    first_letter = alphabet[sum_digits]

    editor = AdvancedTextEditor(root)
    root.mainloop()
👁️ Viewed: 5

Comments