Advertisement
CodeCrusader

Text Editor

Jun 10th, 2023
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.23 KB | Software | 0 0
  1. import tkinter as tk
  2. from tkinter import filedialog
  3.  
  4. # Initialize the window
  5. window = tk.Tk()
  6. window.title("Text Editor")
  7. window.geometry("500x500")
  8.  
  9. # Text widget for text entry
  10. text_widget = tk.Text(window, height=25, width=60)
  11. text_widget.pack()
  12.  
  13. # Function to open a file
  14. def open_file():
  15.     file_path = filedialog.askopenfilename(filetypes=[("Text Files", "*.txt")])
  16.     if file_path:
  17.         with open(file_path, "r") as file:
  18.             content = file.read()
  19.             text_widget.delete("1.0", tk.END)
  20.             text_widget.insert(tk.END, content)
  21.  
  22. # Function to save a file
  23. def save_file():
  24.     file_path = filedialog.asksaveasfilename(defaultextension=".txt", filetypes=[("Text Files", "*.txt")])
  25.     if file_path:
  26.         content = text_widget.get("1.0", tk.END)
  27.         with open(file_path, "w") as file:
  28.             file.write(content)
  29.  
  30. # Create the menu
  31. menu = tk.Menu(window)
  32. file_menu = tk.Menu(menu, tearoff=0)
  33. file_menu.add_command(label="Open", command=open_file)
  34. file_menu.add_command(label="Save", command=save_file)
  35. file_menu.add_separator()
  36. file_menu.add_command(label="Exit", command=window.quit)
  37. menu.add_cascade(label="File", menu=file_menu)
  38. window.config(menu=menu)
  39.  
  40. window.mainloop()
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement