-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathText_editor.py
More file actions
50 lines (40 loc) · 1.41 KB
/
Copy pathText_editor.py
File metadata and controls
50 lines (40 loc) · 1.41 KB
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
from tkinter import *
from tkinter.filedialog import askopenfilename,asksaveasfilename
window = Tk()
window.title("Codingal's Text Editor")
window.geometry("600x500")
window.rowconfigure(0,minsize=800,weight=1)
window.columnconfigure(1,minsize=800,weight=1)
def open_file():
"""Open a file for editing"""
filepath = askopenfilename(
filetypes=[("Text Files","*.txt"),("All Files","*.*")]
)
if not filepath:
return
txt_edit.delete(1.0,END)
with open(filepath,"r") as input_file:
text = input_file.read()
txt_edit.insert(END,text)
input_file.close()
window.title(f"Codingal's Text Editor = {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,END)
output_file.write(text)
window.title(f"Codingal's text editor - {filepath}")
txt_edit = Text(window)
fr_buttons = Frame(window,relief=RAISED,bd=2)
btn_open = Button(fr_buttons,text="Open",command=open_file)
btn_save = Button(fr_buttons,text="Save As...",command=save_file)
btn_open.grid(row=0,column=0,sticky="ew",padx=5,pady=5)
btn_save.grid(row=1,column=0,sticky="ew",padx=5)
fr_buttons.grid(row=0,column=0,sticky="ns")
txt_edit.grid(row=0,column=1,sticky="nsew")
window.mainloop()