|
| 1 | +# Import |
| 2 | +from tkinter import * |
| 3 | +from tkinter import scrolledtext |
| 4 | +from tkinter import filedialog |
| 5 | +import ctypes |
| 6 | +import sys |
| 7 | + |
| 8 | +# Increas Dots Per inch so it looks sharper |
| 9 | +ctypes.windll.shcore.SetProcessDpiAwareness(True) |
| 10 | + |
| 11 | +# Setup Variables |
| 12 | + |
| 13 | +appName = 'Simple Text Editor' |
| 14 | +nofileOpenedString = 'New File' |
| 15 | + |
| 16 | +currentFilePath = nofileOpenedString |
| 17 | + |
| 18 | +# Viable File Types, when opening and saving files. |
| 19 | +fileTypes = [("Text Files","*.txt"), ("Markdown","*.md")] |
| 20 | + |
| 21 | +# Tkinter Setup |
| 22 | +window = Tk() |
| 23 | + |
| 24 | +# Set the first column to occupy 100% of the width |
| 25 | +window.grid_columnconfigure(0, weight=1) |
| 26 | + |
| 27 | +window.title(appName + " - " + currentFilePath) |
| 28 | + |
| 29 | +# Window Dimensions in Pixel |
| 30 | +window.geometry('500x400') |
| 31 | + |
| 32 | +# Handler Functions |
| 33 | +def fileDropDownHandeler(action): |
| 34 | + global currentFilePath |
| 35 | + |
| 36 | + # Opening a File |
| 37 | + if action == "open": |
| 38 | + file = filedialog.askopenfilename(filetypes = fileTypes) |
| 39 | + |
| 40 | + window.title(appName + " - " + file) |
| 41 | + |
| 42 | + currentFilePath = file |
| 43 | + |
| 44 | + with open(file, 'r') as f: |
| 45 | + txt.delete(1.0,END) |
| 46 | + txt.insert(INSERT,f.read()) |
| 47 | + |
| 48 | + # Making a new File |
| 49 | + elif action == "new": |
| 50 | + currentFilePath = nofileOpenedString |
| 51 | + txt.delete(1.0,END) |
| 52 | + window.title(appName + " - " + currentFilePath) |
| 53 | + |
| 54 | + # Saving a file |
| 55 | + elif action == "save" or action == "saveAs": |
| 56 | + if currentFilePath == nofileOpenedString or action== 'saveAs': |
| 57 | + currentFilePath = filedialog.asksaveasfilename(filetypes = fileTypes) |
| 58 | + |
| 59 | + with open(currentFilePath, 'w') as f: |
| 60 | + f.write(txt.get('1.0','end')) |
| 61 | + |
| 62 | + window.title(appName + " - " + currentFilePath) |
| 63 | + |
| 64 | +def textchange(event): |
| 65 | + window.title(appName + " - *" + currentFilePath) |
| 66 | + |
| 67 | +# Widgets |
| 68 | + |
| 69 | +# Text Area |
| 70 | +txt = scrolledtext.ScrolledText(window, height=999) |
| 71 | +txt.grid(row=1,sticky=N+S+E+W) |
| 72 | + |
| 73 | +# Bind event in the widget to a function |
| 74 | +txt.bind('<KeyPress>', textchange) |
| 75 | + |
| 76 | +# Menu |
| 77 | +menu = Menu(window) |
| 78 | + |
| 79 | +# set tearoff to 0 |
| 80 | +fileDropdown = Menu(menu, tearoff=False) |
| 81 | + |
| 82 | +# Add Commands and and their callbacks |
| 83 | +fileDropdown.add_command(label='New', command=lambda: fileDropDownHandeler("new")) |
| 84 | +fileDropdown.add_command(label='Open', command=lambda: fileDropDownHandeler("open")) |
| 85 | + |
| 86 | +# Adding a seperator between button types. |
| 87 | +fileDropdown.add_separator() |
| 88 | +fileDropdown.add_command(label='Save', command=lambda: fileDropDownHandeler("save")) |
| 89 | +fileDropdown.add_command(label='Save as', command=lambda: fileDropDownHandeler("saveAs")) |
| 90 | + |
| 91 | +menu.add_cascade(label='File', menu=fileDropdown) |
| 92 | + |
| 93 | +# Set Menu to be Main Menu |
| 94 | +window.config(menu=menu) |
| 95 | + |
| 96 | +# Enabling "open with" by looking if the second argument was passed. |
| 97 | +if len(sys.argv) == 2: |
| 98 | + currentFilePath = sys.argv[1] |
| 99 | + |
| 100 | + window.title(appName + " - " + currentFilePath) |
| 101 | + |
| 102 | + with open(currentFilePath, 'r') as f: |
| 103 | + txt.delete(1.0,END) |
| 104 | + txt.insert(INSERT,f.read()) |
| 105 | + |
| 106 | +# Main Loop |
| 107 | +window.mainloop() |
0 commit comments