|
| 1 | +#!/usr/bin/python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +import tkinter as tk |
| 4 | +from tkinter import Tk |
| 5 | +from tkinter import font, filedialog, messagebox as mbox |
| 6 | + |
| 7 | +from click import FileError |
| 8 | +from markdown2 import Markdown |
| 9 | +from tkhtmlview import HTMLLabel |
| 10 | + |
| 11 | + |
| 12 | +class Window(tk.Frame): |
| 13 | + def __init__(self, master=None): |
| 14 | + tk.Frame.__init__(self, master) |
| 15 | + self.master = master |
| 16 | + self.font = font.Font(family="Helvetica", size=14) |
| 17 | + self.init_window() |
| 18 | + |
| 19 | + def onChange(self, event): |
| 20 | + self.inputeditor.edit_modified(0) |
| 21 | + md2html = Markdown() |
| 22 | + self.outputbox.set_html(md2html.convert( |
| 23 | + self.inputeditor.get("1.0", tk.END))) |
| 24 | + |
| 25 | + def openfile(self): |
| 26 | + filename = filedialog.askopenfilename( |
| 27 | + filetypes=[ |
| 28 | + ("Markdown File", "*.md *.mdown *markdown"), |
| 29 | + ("Text file", "*.txt"), |
| 30 | + ("All files", "*.*"), |
| 31 | + ] |
| 32 | + ) |
| 33 | + if filename: |
| 34 | + try: |
| 35 | + self.inputeditor.delete("1.0", tk.END) |
| 36 | + self.inputeditor.insert(tk.END, open(filename, "r").read()) |
| 37 | + except FileError: |
| 38 | + mbox.showerror( |
| 39 | + "Error opening file", "{} \ |
| 40 | + cannot be opened !".format( |
| 41 | + filename |
| 42 | + ), |
| 43 | + ) |
| 44 | + |
| 45 | + def savefile(self): |
| 46 | + filedata = self.inputeditor.get("1.0", tk.END) |
| 47 | + filename = filedialog.asksaveasfilename( |
| 48 | + filetypes=(("Markdown file", "*.md"), ("Text file", "*.txt")), |
| 49 | + title="Save Markdown File", |
| 50 | + ) |
| 51 | + if filename: |
| 52 | + try: |
| 53 | + f = open(filename, "w") |
| 54 | + f.write(filedata) |
| 55 | + except FileError: |
| 56 | + mbox.showerror("Error saving file", "Unable to save the file") |
| 57 | + |
| 58 | + def init_window(self): |
| 59 | + self.master.title("Mardown Viewer") |
| 60 | + self.pack(fill=tk.BOTH, expand=1) |
| 61 | + |
| 62 | + self.mainmenu = tk.Menu(self) |
| 63 | + self.filemenu = tk.Menu(self.mainmenu) |
| 64 | + self.filemenu.add_command(label="Open", command=self.openfile) |
| 65 | + self.filemenu.add_command(label="Save as", command=self.savefile) |
| 66 | + self.filemenu.add_separator() |
| 67 | + self.filemenu.add_command(label="Exit", command=self.quit) |
| 68 | + self.mainmenu.add_cascade(label="File", menu=self.filemenu) |
| 69 | + self.master.config(menu=self.mainmenu) |
| 70 | + |
| 71 | + self.inputeditor = tk.Text(self, width="1", font=self.font) |
| 72 | + self.inputeditor.pack(fill=tk.BOTH, expand=1, side=tk.LEFT) |
| 73 | + self.inputeditor.bind("<<Modified>>", self.onChange) |
| 74 | + |
| 75 | + self.outputbox = HTMLLabel( |
| 76 | + self, width="1", background="white", |
| 77 | + html="<h1>Markdown Editor</h1>" |
| 78 | + ) |
| 79 | + self.outputbox.pack(fill=tk.BOTH, expand=1, side=tk.RIGHT) |
| 80 | + |
| 81 | + |
| 82 | +root = Tk() |
| 83 | +root.geometry("750x600") |
| 84 | +app = Window(root) |
| 85 | +app.mainloop() |
0 commit comments