Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- from tkinter import filedialog
- # Initialize the window
- window = tk.Tk()
- window.title("Text Editor")
- window.geometry("500x500")
- # Text widget for text entry
- text_widget = tk.Text(window, height=25, width=60)
- text_widget.pack()
- # Function to open a file
- def open_file():
- file_path = filedialog.askopenfilename(filetypes=[("Text Files", "*.txt")])
- if file_path:
- with open(file_path, "r") as file:
- content = file.read()
- text_widget.delete("1.0", tk.END)
- text_widget.insert(tk.END, content)
- # Function to save a file
- def save_file():
- file_path = filedialog.asksaveasfilename(defaultextension=".txt", filetypes=[("Text Files", "*.txt")])
- if file_path:
- content = text_widget.get("1.0", tk.END)
- with open(file_path, "w") as file:
- file.write(content)
- # Create the menu
- menu = tk.Menu(window)
- file_menu = tk.Menu(menu, tearoff=0)
- 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=window.quit)
- menu.add_cascade(label="File", menu=file_menu)
- window.config(menu=menu)
- window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement