MagnificentMarkdownManipulator Python GUI

👤 Sharing: AI
import tkinter as tk
from tkinter import filedialog, scrolledtext, messagebox
import markdown
import os

class MarkdownEditor:
    def __init__(self, master):
        self.master = master
        master.title("Magnificent Markdown Manipulator")

        self.text_area = scrolledtext.ScrolledText(master, wrap=tk.WORD, font=("Arial", 12))
        self.text_area.pack(expand=True, fill=tk.BOTH)

        self.create_menu()

        self.details_button = tk.Button(master, text="Details", command=self.show_details)
        self.details_button.pack()

    def create_menu(self):
        menubar = tk.Menu(self.master)
        filemenu = tk.Menu(menubar, tearoff=0)
        filemenu.add_command(label="Open", command=self.open_file)
        filemenu.add_command(label="Save", command=self.save_file)
        filemenu.add_command(label="Save As", command=self.save_as_file)
        filemenu.add_separator()
        filemenu.add_command(label="Export to HTML", command=self.export_to_html)
        filemenu.add_separator()
        filemenu.add_command(label="Exit", command=self.master.quit)
        menubar.add_cascade(label="File", menu=filemenu)

        self.master.config(menu=menubar)

    def open_file(self):
        filepath = filedialog.askopenfilename(filetypes=[("Markdown Files", "*.md;*.markdown"), ("Text Files", "*.txt"), ("All Files", "*")])
        if filepath:
            with open(filepath, "r", encoding="utf-8") as f:
                self.text_area.delete("1.0", tk.END)
                self.text_area.insert(tk.END, f.read())

    def save_file(self):
        if hasattr(self, 'current_file') and self.current_file:
            self._save_file(self.current_file)
        else:
            self.save_as_file()

    def save_as_file(self):
        filepath = filedialog.asksaveasfilename(defaultextension=".md", filetypes=[("Markdown Files", "*.md;*.markdown"), ("Text Files", "*.txt"), ("All Files", "*")])
        if filepath:
            self._save_file(filepath)
            self.current_file = filepath

    def _save_file(self, filepath):
        try:
            with open(filepath, "w", encoding="utf-8") as f:
                f.write(self.text_area.get("1.0", tk.END))
        except Exception as e:
            messagebox.showerror("Error", f"Could not save file: {e}")

    def export_to_html(self):
        filepath = filedialog.asksaveasfilename(defaultextension=".html", filetypes=[("HTML Files", "*.html"), ("All Files", "*")])
        if filepath:
            text = self.text_area.get("1.0", tk.END)
            html = markdown.markdown(text)
            try:
                with open(filepath, "w", encoding="utf-8") as f:
                    f.write(html)
            except Exception as e:
                messagebox.showerror("Error", f"Could not export to HTML: {e}")

    def show_details(self):
        messagebox.showinfo("Details", "This program is a simple Markdown editor with basic file operations (Open, Save, Save As, Export to HTML).  It allows users to create, edit, and save Markdown files, as well as export them to HTML format. The GUI is built using Tkinter and the Markdown conversion is done using the 'markdown' library.")

root = tk.Tk()
editor = MarkdownEditor(root)
root.mainloop()
👁️ Viewed: 8

Comments