Created
October 5, 2019 14:23
-
-
Save maksimKorzh/66573bef9c539c5ef815525eab1fafc9 to your computer and use it in GitHub Desktop.
Minimalist python tkinter text editor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Simple code editor with syntax highlighting | |
# | |
# import Libraries | |
import tkinter as tk | |
from tkinter import ttk | |
from tkinter import filedialog | |
# create new file | |
def new_file(): | |
# if text wasn't changed | |
if not text_area.edit_modified(): | |
# clear text area | |
text_area.delete('1.0', tk.END) | |
# otherwise | |
else: | |
# call 'save as' function | |
save_file_as() | |
# clear text area after | |
text_area.delete('1.0', tk.END) | |
# clear 'is_modified' flag | |
text_area.edit_modified(0) | |
# restore window title | |
main_window.title('Notepad') | |
# open existing file | |
def open_file(): | |
# if text wasn't changed | |
if not text_area.edit_modified(): | |
# try to open file | |
try: | |
# get the path of file to open | |
path = filedialog.askopenfile(filetypes = (("Text files", "*.txt"), ("All files", "*.*"))).name | |
# store path in window title for later reference | |
main_window.title('Notepad - ' + path) | |
# open file stream | |
with open(path, 'r') as f: | |
# clear text area and insert file content | |
content = f.read() | |
text_area.delete('1.0', tk.END) | |
text_area.insert('1.0', content) | |
# clear 'is_modified' flag | |
text_area.edit_modified(0) | |
# exception is thrown if 'Cancel' button is clicked on 'Open file dialogue' | |
except: | |
pass | |
# otherwise | |
else: | |
# call 'save as' function | |
save_file_as() | |
# clear 'is_modified' flag | |
text_area.edit_modified(0) | |
# call 'open_file' function recursively | |
open_file() | |
# save current file | |
def save_file(): | |
# try to get current file path | |
try: | |
# get file path from window title | |
path = main_window.title().split('-')[1][1:] | |
# init path variable to empty string on exception | |
except: | |
path = '' | |
# check if file path is available | |
if path != '': | |
# write file | |
with open(path, 'w') as f: | |
content = text_area.get('1.0', tk.END) | |
f.write(content) | |
# otherwise call 'save as' function | |
else: | |
save_file_as() | |
# clear 'is_modified' flag | |
text_area.edit_modified(0) | |
def save_file_as(): | |
# try to get file path | |
try: | |
path = filedialog.asksaveasfile(filetypes = (("Text files", "*.txt"), ("All files", "*.*"))).name | |
main_window.title('Notepad - ' + path) | |
# return if 'Cancel' button is clicked on 'Save file dialogue' | |
except: | |
return | |
# otherwise write file to disk | |
with open(path, 'w') as f: | |
f.write(text_area.get('1.0', tk.END)) | |
# create main window instance | |
main_window = tk.Tk() | |
# configure main window | |
main_window.title('Notepad') | |
main_window.geometry('800x600') | |
# create menu bar instance | |
menubar = tk.Menu(main_window) | |
# create 'File' menu items | |
file_menu = tk.Menu(menubar, tearoff=0) | |
file_menu.add_command(label="New", command=new_file) | |
file_menu.add_command(label="Open", command=open_file) | |
file_menu.add_command(label="Save", command=save_file) | |
file_menu.add_command(label="Save as...", command=save_file_as) | |
file_menu.add_separator() | |
file_menu.add_command(label="Exit", command=main_window.quit) | |
# add 'File' menu to the menu bar | |
menubar.add_cascade(label="File", menu=file_menu) | |
# create text area to input text | |
text_area = tk.Text(main_window) | |
text_area.pack(expand = tk.YES, fill = tk.BOTH, side = tk.LEFT) | |
# create scrollbar and link it to the text area | |
scroll_bar = ttk.Scrollbar(main_window, orient=tk.VERTICAL, command=text_area.yview) | |
scroll_bar.pack(fill=tk.Y, side=tk.RIGHT) | |
text_area['yscrollcommand'] = scroll_bar.set | |
# Connect menubar to the window | |
main_window.config(menu=menubar) | |
# run main application loop | |
tk.mainloop() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment