-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextEditor.py
More file actions
51 lines (36 loc) · 1.4 KB
/
TextEditor.py
File metadata and controls
51 lines (36 loc) · 1.4 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
49
50
51
import tkinter as tk
from tkinter.filedialog import askopenfilename, asksaveasfilename
from types import LambdaType
def open_file(window, text_edit):
filepath = askopenfilename(filetypes=[("Text Files", "*.txt")])
if not filepath:
return
text_edit.delete(1.0, tk.END)
with open(filepath, "r") as file:
content = file.read()
text_edit.insert(tk.END, content)
window.title(f"Open File: {filepath}")
def save_file(window, text_edit):
filepath = asksaveasfilename(filetypes=[("Text Files", "*.txt")])
if not filepath:
return
with open(filepath, "w") as file:
content = text_edit.get(1.0, tk.END)
file.write(content)
window.title(f"Open File: {filepath}")
def main():
window = tk.Tk()
window.title("Text Editor")
window.rowconfigure(0, minsize=400)
window.columnconfigure(1, minsize=500)
text_edit = tk.Text(window)
text_edit.grid(row=0, column=1)
frame = tk.Frame(window, relief=tk.RAISED, bd=2)
save_button = tk.Button(frame, text="Save", command=lambda :save_file(window, text_edit))
open_button = tk.Button(frame, text="Open", command=lambda :open_file(window, text_edit))
save_button.grid(row=0, column=0, padx=5, pady=5, sticky="ew")
open_button.grid(row=1, column=0, padx=5, sticky="ew")
frame.grid(row=0, column=0, sticky="ns")
window.mainloop()
if __name__ == "__main__":
main()