Creating a text editor in Python can be a fun and educational project. Here, we'll build a basic text editor using Tkinter, a standard GUI library in Python. This text editor will have features such as creating, opening, saving, and editing text files.
First, you need to import Tkinter and set up the main application window.
import tkinter as tk
from tkinter import filedialog, messagebox
# Create the main application window
root = tk.Tk()
root.title("Simple Text Editor")
root.geometry("800x600")
# Create a Text widget
text_area = tk.Text(root, wrap='word')
text_area.pack(expand=1, fill='both')
Next, add menu options for creating a new file, opening an existing file, saving the current file, and exiting the application.
# Create a menu bar
menu_bar = tk.Menu(root)
# Create File menu
file_menu = tk.Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label="File", menu=file_menu)
def new_file():
text_area.delete(1.0, tk.END)
def open_file():
file_path = filedialog.askopenfilename(defaultextension=".txt",
filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")])
if file_path:
with open(file_path, 'r') as file:
text_area.delete(1.0, tk.END)
text_area.insert(1.0, file.read())
def save_file():
file_path = filedialog.asksaveasfilename(defaultextension=".txt",
filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")])
if file_path:
with open(file_path, 'w') as file:
file.write(text_area.get(1.0, tk.END))
def exit_editor():
if messagebox.askokcancel("Quit", "Do you really want to quit?"):
root.quit()
# Add menu items to 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", command=save_file)
file_menu.add_separator()
file_menu.add_command(label="Exit", command=exit_editor)
# Configure the menu bar
root.config(menu=menu_bar)
Add shortcut keys for the menu options to make the editor more user-friendly.
# Bind shortcut keys
root.bind('<Control-n>', lambda event: new_file())
root.bind('<Control-o>', lambda event: open_file())
root.bind('<Control-s>', lambda event: save_file())
root.bind('<Control-q>', lambda event: exit_editor())
Add vertical and horizontal scrollbars to the text area.
# Add a scrollbar to the Text widget
scroll_bar = tk.Scrollbar(text_area)
scroll_bar.pack(side=tk.RIGHT, fill=tk.Y)
text_area.config(yscrollcommand=scroll_bar.set)
scroll_bar.config(command=text_area.yview)
scroll_bar_x = tk.Scrollbar(text_area, orient=tk.HORIZONTAL)
scroll_bar_x.pack(side=tk.BOTTOM, fill=tk.X)
text_area.config(xscrollcommand=scroll_bar_x.set)
scroll_bar_x.config(command=text_area.xview)
Run the main event loop to start the application.
if __name__ == "__main__":
root.mainloop()
Here's the complete code for the basic text editor:
import tkinter as tk
from tkinter import filedialog, messagebox
# Create the main application window
root = tk.Tk()
root.title("Simple Text Editor")
root.geometry("800x600")
# Create a Text widget
text_area = tk.Text(root, wrap='word')
text_area.pack(expand=1, fill='both')
# Create a menu bar
menu_bar = tk.Menu(root)
# Create File menu
file_menu = tk.Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label="File", menu=file_menu)
def new_file():
text_area.delete(1.0, tk.END)
def open_file():
file_path = filedialog.askopenfilename(defaultextension=".txt",
filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")])
if file_path:
with open(file_path, 'r') as file:
text_area.delete(1.0, tk.END)
text_area.insert(1.0, file.read())
def save_file():
file_path = filedialog.asksaveasfilename(defaultextension=".txt",
filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")])
if file_path:
with open(file_path, 'w') as file:
file.write(text_area.get(1.0, tk.END))
def exit_editor():
if messagebox.askokcancel("Quit", "Do you really want to quit?"):
root.quit()
# Add menu items to 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", command=save_file)
file_menu.add_separator()
file_menu.add_command(label="Exit", command=exit_editor)
# Configure the menu bar
root.config(menu=menu_bar)
# Bind shortcut keys
root.bind('<Control-n>', lambda event: new_file())
root.bind('<Control-o>', lambda event: open_file())
root.bind('<Control-s>', lambda event: save_file())
root.bind('<Control-q>', lambda event: exit_editor())
# Add a scrollbar to the Text widget
scroll_bar = tk.Scrollbar(text_area)
scroll_bar.pack(side=tk.RIGHT, fill=tk.Y)
text_area.config(yscrollcommand=scroll_bar.set)
scroll_bar.config(command=text_area.yview)
scroll_bar_x = tk.Scrollbar(text_area, orient=tk.HORIZONTAL)
scroll_bar_x.pack(side=tk.BOTTOM, fill=tk.X)
text_area.config(xscrollcommand=scroll_bar_x.set)
scroll_bar_x.config(command=text_area.xview)
# Run the main event loop
if __name__ == "__main__":
root.mainloop()