import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
from tkinter import font as tkfont
def open_file():
file_path = filedialog.askopenfilename()
if file_path:
with open(file_path, ‘r’) as file:
text.delete(‘1.0’, tk.END)
text.insert(tk.END, file.read())
def save_file():
file_path = filedialog.asksaveasfilename(defaultextension=”.txt”)
if file_path:
with open(file_path, ‘w’) as file:
file.write(text.get(‘1.0’, tk.END))
def save_file_as():
file_path = filedialog.asksaveasfilename(defaultextension=”.txt”)
if file_path:
with open(file_path, ‘w’) as file:
file.write(text.get(‘1.0’, tk.END))
def copy_text():
text.clipboard_clear()
text.clipboard_append(text.selection_get())
def cut_text():
copy_text()
text.delete(‘sel.first’, ‘sel.last’)
def paste_text():
text.insert(‘insert’, text.clipboard_get())
def undo():
try:
text.edit_undo()
except Exception:
pass
def redo():
try:
text.edit_redo()
except Exception:
pass
def change_font():
selected_font = font_var.get()
text.config(font=(selected_font, font_size_var.get()))
def change_color():
selected_color = color_var.get()
text.config(fg=selected_color)
def print_text():
try:
text.printout()
except Exception as e:
messagebox.showerror(“Error”, f”Printing failed: {str(e)}”)
# Create the main window
root = tk.Tk()
root.title(“Notepad”)
# Create a Text widget for editing
text = tk.Text(root)
text.pack()
# Create a menu bar
menu_bar = tk.Menu(root)
root.config(menu=menu_bar)
# Create a File menu
file_menu = tk.Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label=”File”, menu=file_menu)
file_menu.add_command(label=”Open”, command=open_file)
file_menu.add_command(label=”Save”, command=save_file)
file_menu.add_command(label=”Save As”, command=save_file_as)
file_menu.add_separator()
file_menu.add_command(label=”Print”, command=print_text)
file_menu.add_separator()
file_menu.add_command(label=”Exit”, command=root.quit)
# Create an Edit menu
edit_menu = tk.Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label=”Edit”, menu=edit_menu)
edit_menu.add_command(label=”Undo”, command=undo)
edit_menu.add_command(label=”Redo”, command=redo)
edit_menu.add_separator()
edit_menu.add_command(label=”Copy”, command=copy_text)
edit_menu.add_command(label=”Cut”, command=cut_text)
edit_menu.add_command(label=”Paste”, command=paste_text)
# Create a View menu
view_menu = tk.Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label=”View”, menu=view_menu)
font_var = tkfont.Font(family=”Arial”, size=12)
font_size_var = tk.StringVar()
font_size_var.set(font_var.actual()[“size”])
font_menu = tk.Menu(view_menu, tearoff=0)
view_menu.add_cascade(label=”Font”, menu=font_menu)
font_menu.add_radiobutton(label=”Arial”, variable=font_var, value=”Arial”, command=change_font)
font_menu.add_radiobutton(label=”Times New Roman”, variable=font_var, value=”Times New Roman”, command=change_font)
font_menu.add_radiobutton(label=”Courier New”, variable=font_var, value=”Courier New”, command=change_font)
font_menu.add_radiobutton(label=”Verdana”, variable=font_var, value=”Verdana”, command=change_font)
font_size_menu = tk.Menu(view_menu, tearoff=0)
view_menu.add_cascade(label=”Font Size”, menu=font_size_menu)
font_size_menu.add_radiobutton(label=”10″, variable=font_size_var, value=”10″, command=change_font)
font_size_menu.add_radiobutton(label=”12″, variable=font_size_var, value=”12″, command=change_font)
font_size_menu.add_radiobutton(label=”14″, variable=font_size_var, value=”14″, command=change_font)
font_size_menu.add_radiobutton(label=”16″, variable=font_size_var, value=”16″, command=change_font)
color_var = tk.StringVar()
color_var.set(“black”)
color_menu = tk.Menu(view_menu, tearoff=0)
view_menu.add_cascade(label=”Text Color”, menu=color_menu)
color_menu.add_radiobutton(label=”Black”, variable=color_var, value=”black”, command=change_color)
color_menu.add_radiobutton(label=”Red”, variable=color_var, value=”red”, command=change_color)
color_menu.add_radiobutton(label=”Blue”, variable=color_var, value=”blue”, command=change_color)
# Start the main event loop
root.mainloop()