import tkinter as tk
from tkinter import filedialog, Text
from tkinter.scrolledtext import ScrolledText
def new_file():
text_area.delete('1.0', tk.END)
def open_file():
file_path = filedialog.askopenfilename(defaultextension=".txt",
filetypes=[("Text Documents", "*.txt"),
("All Files", "*.*")])
if file_path:
with open(file_path, "r", encoding='utf-8') as file:
content = file.read()
text_area.delete('1.0', tk.END)
text_area.insert(tk.END, content)
def save_file():
file_path = filedialog.asksaveasfilename(defaultextension=".txt",
filetypes=[("Text Documents", "*.txt"),
("All Files", "*.*")])
if file_path:
with open(file_path, "w", encoding='utf-8') as file:
content = text_area.get('1.0', tk.END)
file.write(content)
root = tk.Tk()
root.title("Simple Word Processor")
text_area = ScrolledText(root, wrap=tk.WORD, undo=True)
text_area.pack(fill=tk.BOTH, expand=1)
menu_bar = tk.Menu(root)
root.config(menu=menu_bar)
file_menu = tk.Menu(menu_bar, tearoff=0)
menu_bar.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 As...", command=save_file)
file_menu.add_separator()
file_menu.add_command(label="Exit", command=root.quit)
root.mainloop()