-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_editor.py
55 lines (45 loc) · 1.76 KB
/
text_editor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import tkinter as tk
from tkinter.filedialog import askopenfilename, asksaveasfilename
window = tk.Tk()
window.title("SI Code")
window.columnconfigure(0, weight=0, minsize=250)
window.columnconfigure(1, weight=1, minsize=400)
window.rowconfigure(0, weight=1, minsize=600)
# event handlers
def open_file():
filepath = askopenfilename(
filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")]
)
if not filepath:
return
txt_edit.delete("1.0", tk.END)
with open(filepath, "r") as input_file:
text = input_file.read()
txt_edit.insert(tk.END, text)
window.title(f"SI Code - {filepath}")
def save_file():
filepath = asksaveasfilename(
defaultextension="txt",
filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")],
)
if not filepath:
return
with open(filepath, "w") as output_file:
text = txt_edit.get("1.0", tk.END)
output_file.write(text)
window.title(f"SI Code - {filepath}")
# frames
left__Frame = tk.Frame(master=window, bg="white", width=200, padx=10, pady=15)
# components
txt_edit = tk.Text(window, bg="#eaeaea", fg="#222",
borderwidth=0, relief=tk.FLAT, padx=10, pady=10)
btn_open = tk.Button(left__Frame, text="Open",
padx=5, pady=5, width=31, relief=tk.FLAT, bg="#eaeaea", command=open_file)
btn_save = tk.Button(left__Frame, text="Save As...",
padx=5, pady=5, width=31, relief=tk.FLAT, bg="#eaeaea", command=save_file)
# packing everything up
txt_edit.grid(row=0, column=1, sticky="nesw")
left__Frame.grid(row=0, column=0, sticky="nesw")
btn_open.grid(row=0, column=0, sticky="nesw")
btn_save.grid(row=1, column=0, sticky="nesw")
window.mainloop()