-
Notifications
You must be signed in to change notification settings - Fork 0
/
editor.py
64 lines (50 loc) · 1.72 KB
/
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
56
57
58
59
60
61
62
63
64
import tkinter as tk
from tkinter import filedialog
def clear_text_box():
text_box.delete(1.0, tk.END)
window.title('Text Editor')
def open_file():
filepath = filedialog.askopenfilename(
filetypes=[('Text Files', '.txt'), ('All Files', '*.*')])
if not filepath:
return
clear_text_box()
with open(filepath, 'r') as file:
text_box.insert(tk.END, file.read())
window.title(f'Text Editor - {filepath}')
def save_file_as():
filepath = filedialog.asksaveasfilename(
defaultextension='.txt',
filetypes=[('Text File', '.txt')]
)
if not filepath:
return
with open(filepath, 'w') as file:
file.write(text_box.get(1.0, tk.END))
window.title(f'Text Editor - {filepath}')
window = tk.Tk()
window.title('Text Editor')
window.geometry('800x800')
button_frame = tk.Frame(master=window, width=50)
button_frame['relief'] = tk.GROOVE
button_frame['borderwidth'] = 2
button_frame.pack(side=tk.LEFT, fill=tk.Y)
editor_frame = tk.Frame(master=window)
editor_frame['relief'] = tk.GROOVE
editor_frame['borderwidth'] = 2
editor_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
open_button = tk.Button(master=button_frame)
open_button['text'] = 'Open'
open_button.pack(fill=tk.X, padx=5, pady=5)
open_button['command'] = open_file
save_as_button = tk.Button(master=button_frame)
save_as_button['text'] = 'Save As...'
save_as_button.pack(fill=tk.X, padx=5, pady=5)
save_as_button['command'] = save_file_as
clear_button = tk.Button(master=button_frame)
clear_button['text'] = 'Clear'
clear_button.pack(fill=tk.X, padx=5, pady=5)
clear_button['command'] = clear_text_box
text_box = tk.Text(master=editor_frame)
text_box.pack(fill=tk.BOTH, expand=True)
window.mainloop()