-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNotepad.py
70 lines (54 loc) · 2.52 KB
/
Notepad.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
65
66
67
68
69
70
import tkinter as tk
from tkinter import N, filedialog
from tkinter import Tk, Text, Frame, Button
class SimpleNotepad:
def __init__(self, root: Tk) -> None:
self.root = root
self.root.title('Sourish\'s Notepad')
self.text_area: Text = Text(self.root, wrap='word')
self.text_area.pack(expand=True, fill='both')
self.button_frame: Frame = Frame(self.root)
self.button_frame.pack(ipady=10)
self.new_button: Button = Button(self.button_frame, text='New', command=self.new_file)
self.new_button.pack(side=tk.LEFT,padx=5)
self.save_button: Button = Button(self.button_frame, text='Save', command=self.save_file)
self.save_button.pack(side=tk.LEFT,padx=5)
self.load_button: Button = Button(self.button_frame, text='Load', command=self.load_file)
self.load_button.pack(side=tk.LEFT,padx=5)
self.file_path = None
def save_file(self) -> None:
if self.file_path:
with open(self.file_path, 'w') as file:
file.write(self.text_area.get(1.0, tk.END))
print(f'File saved to: {self.file_path}')
else:
self.file_path = filedialog.asksaveasfilename(defaultextension='.txt',
filetypes=[('Text files', '*.txt')])
with open(self.file_path, 'w') as file:
file.write(self.text_area.get(1.0, tk.END))
print(f'File saved to: {self.file_path}')
def load_file(self) -> None:
file_path: str = filedialog.askopenfilename(defaultextension='.txt',
filetypes=[('Text files', '*.txt')])
with open(file_path, 'r') as file:
content: str = file.read()
self.text_area.delete(1.0, tk.END)
self.text_area.insert(tk.INSERT, content)
self.file_path = file_path
print(f'File loaded from: {self.file_path}')
def new_file(self) -> None:
file_path: str = filedialog.asksaveasfilename(defaultextension='.txt',
filetypes=[('Text files', '*.txt')])
with open(file_path, 'w') as file:
file.write('')
self.text_area.delete(1.0, tk.END)
self.file_path = file_path
print(f'File created at: {self.file_path}')
def run(self) -> None:
self.root.mainloop()
def main() -> None:
root: Tk = tk.Tk()
app: SimpleNotepad = SimpleNotepad(root=root)
app.run()
if __name__ == '__main__':
main()