-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadnotepad.py
130 lines (106 loc) · 4.79 KB
/
adnotepad.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
from tkinter import *
from tkinter import filedialog, font, colorchooser
# Functions for File Operations
def new_file():
text_area.delete(1.0, END)
def open_file():
file = filedialog.askopenfile(mode="r", filetypes=[("Text Files", "*.txt")])
if file:
text_area.delete(1.0, END)
text_area.insert(END, file.read())
file.close()
def save_file():
file = filedialog.asksaveasfile(mode="w", defaultextension=".txt", filetypes=[("Text Files", "*.txt")])
if file:
file.write(text_area.get(1.0, END))
file.close()
# Text Formatting Functions
def make_bold():
current_font = font.Font(text_area, text_area.cget("font"))
text_area.config(font=(current_font.actual()["family"], current_font.actual()["size"], "bold"))
def make_italic():
current_font = font.Font(text_area, text_area.cget("font"))
text_area.config(font=(current_font.actual()["family"], current_font.actual()["size"], "italic"))
def make_underline():
current_font = font.Font(text_area, text_area.cget("font"))
text_area.config(font=(current_font.actual()["family"], current_font.actual()["size"], "underline"))
def change_font():
fonts = ["Arial", "Helvetica", "Times New Roman", "Courier", "Comic Sans MS"]
selected_font = filedialog.askstring("Select Font", "Choose Font: " + ", ".join(fonts))
if selected_font and selected_font in fonts:
text_area.config(font=(selected_font, 12))
def change_color():
selected_color = colorchooser.askcolor(title="Choose Text Color")[1]
if selected_color:
text_area.config(fg=selected_color)
def increase_font_size():
current_font = font.Font(text_area, text_area.cget("font"))
new_size = current_font.actual()["size"] + 2
text_area.config(font=(current_font.actual()["family"], new_size))
def decrease_font_size():
current_font = font.Font(text_area, text_area.cget("font"))
new_size = max(8, current_font.actual()["size"] - 2)
text_area.config(font=(current_font.actual()["family"], new_size))
# Find and Replace Feature
def find_replace():
find_window = Toplevel(root)
find_window.title("Find & Replace")
find_window.geometry("300x150")
Label(find_window, text="Find:").grid(row=0, column=0, padx=10, pady=5)
Label(find_window, text="Replace:").grid(row=1, column=0, padx=10, pady=5)
find_entry = Entry(find_window, width=20)
replace_entry = Entry(find_window, width=20)
find_entry.grid(row=0, column=1, padx=10, pady=5)
replace_entry.grid(row=1, column=1, padx=10, pady=5)
def replace_text():
text_content = text_area.get(1.0, END)
find_text = find_entry.get()
replace_text = replace_entry.get()
text_content = text_content.replace(find_text, replace_text)
text_area.delete(1.0, END)
text_area.insert(1.0, text_content)
Button(find_window, text="Replace", command=replace_text).grid(row=2, column=1, padx=10, pady=5)
# Main Window Setup
root = Tk()
root.title("Advanced Notepad")
root.geometry("800x500")
# Creating Menu Bar
menubar = Menu(root)
# File Menu
file_menu = Menu(menubar, tearoff=0)
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)
menubar.add_cascade(label="File", menu=file_menu)
# Edit Menu
edit_menu = Menu(menubar, tearoff=0)
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>>"))
edit_menu.add_separator()
edit_menu.add_command(label="Find & Replace", command=find_replace)
menubar.add_cascade(label="Edit", menu=edit_menu)
# Format Menu
format_menu = Menu(menubar, tearoff=0)
format_menu.add_command(label="Bold", command=make_bold)
format_menu.add_command(label="Italic", command=make_italic)
format_menu.add_command(label="Underline", command=make_underline)
format_menu.add_separator()
format_menu.add_command(label="Change Font", command=change_font)
format_menu.add_command(label="Change Color", command=change_color)
format_menu.add_separator()
format_menu.add_command(label="Increase Font Size", command=increase_font_size)
format_menu.add_command(label="Decrease Font Size", command=decrease_font_size)
menubar.add_cascade(label="Format", menu=format_menu)
# Help Menu
help_menu = Menu(menubar, tearoff=0)
help_menu.add_command(label="About", command=lambda: print("Advanced Notepad by Jamil Ahmed"))
menubar.add_cascade(label="Help", menu=help_menu)
# 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()