-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotepad.py
More file actions
41 lines (33 loc) · 1.32 KB
/
Copy pathnotepad.py
File metadata and controls
41 lines (33 loc) · 1.32 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
import tkinter as tk
from tkinter import filedialog
class Notepad:
def __init__(self, master):
self.master = master
self.master.title("Notepad Clone")
self.text = tk.Text(self.master, undo=True)
self.text.pack(fill=tk.BOTH, expand=1)
self.menu = tk.Menu(self.master)
self.master.config(menu=self.menu)
file_menu = tk.Menu(self.menu)
self.menu.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="New", command=self.new_file)
file_menu.add_command(label="Open", command=self.open_file)
file_menu.add_command(label="Save", command=self.save_file)
file_menu.add_separator()
file_menu.add_command(label="Exit", command=self.master.quit)
def new_file(self):
self.text.delete("1.0", tk.END)
def open_file(self):
file_path = filedialog.askopenfilename()
if file_path:
with open(file_path, "r") as file:
self.text.delete("1.0", tk.END)
self.text.insert("1.0", file.read())
def save_file(self):
file_path = filedialog.asksaveasfilename(defaultextension=".txt")
if file_path:
with open(file_path, "w") as file:
file.write(self.text.get("1.0", tk.END))
root = tk.Tk()
notepad = Notepad(root)
root.mainloop()