-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_notepad.py
58 lines (47 loc) · 1.77 KB
/
simple_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
from tkinter import *
from tkinter import filedialog
def new_file():
text_area.delete(1.0, END)
def open_file():
file = filedialog.askopenfile(mode="r", filetypes=[("Text Files", "*.txt")])
if file:
content = file.read()
text_area.delete(1.0, END)
text_area.insert(END, content)
file.close()
def save_file():
file = filedialog.asksaveasfile(mode="w", defaultextension=".txt", filetypes=[("Text Files", "*.txt")])
if file:
content = text_area.get(1.0, END)
file.write(content)
file.close()
# Creating main window
root = Tk()
root.title("Simple Notepad")
root.geometry("600x400")
# Creating Menubar
menubar = Menu(root)
# File Menu
file_menu = Menu(menubar, tearoff=0)
menubar.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="New", command=new_file)
file_menu.add_command(label="Open", command=open_file)
file_menu.add_command(label="Save", command=save_file)
file_menu.add_separator()
file_menu.add_command(label="Exit", command=root.destroy)
# Adding Edit Menu (basic operations)
edit_menu = Menu(menubar, tearoff=0)
menubar.add_cascade(label="Edit", menu=edit_menu)
edit_menu.add_command(label="Cut", command=lambda: text_area.event_generate("<<Cut>>"))
edit_menu.add_command(label="Copy", command=lambda: text_area.event_generate("<<Copy>>"))
edit_menu.add_command(label="Paste", command=lambda: text_area.event_generate("<<Paste>>"))
# Adding Help Menu
help_menu = Menu(menubar, tearoff=0)
menubar.add_cascade(label="Help", menu=help_menu)
help_menu.add_command(label="About", command=lambda: print("Simple Notepad by Jamil Ahmed"))
# Creating Textbox
text_area = Text(root, wrap="word", font=("Arial", 12))
text_area.pack(expand=True, fill=BOTH)
# Display Menu
root.config(menu=menubar)
root.mainloop()