Build Your Own Python Text Editor in 1 Minute! [Tkinter Project]
Demo :
Click Video πππ
Description: Step-by-step guide to creating your own text editor using Python's Tkinter module. Boost your skills with this easy mini-project!
Code :
import tkinter as tk
from tkinter import filedialog, messagebox
# Create the main window
root = tk.Tk()
root.title("Python Text Editor")
root.geometry("600x400")
# Function to open a file
def open_file():
file = filedialog.askopenfilename(defaultextension=".txt", filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")])
if file:
with open(file, "r") as f:
text_area.delete(1.0, tk.END)
text_area.insert(tk.END, f.read())
# Function to save the file
def save_file():
file = filedialog.asksaveasfilename(defaultextension=".txt", filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")])
if file:
with open(file, "w") as f:
f.write(text_area.get(1.0, tk.END))
# Function to clear text
def clear_text():
text_area.delete(1.0, tk.END)
# Function to display about information
def about():
messagebox.showinfo("About", "Python Text Editor\nVersion 1.0")
# Create a menu bar
menu_bar = tk.Menu(root)
root.config(menu=menu_bar)
# Create 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="Clear", command=clear_text)
file_menu.add_separator()
file_menu.add_command(label="Exit", command=root.quit)
# Create help menu
help_menu = tk.Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label="Help", menu=help_menu)
help_menu.add_command(label="About", command=about)
# Create a text area
text_area = tk.Text(root, wrap="word", font=("Arial", 12))
text_area.pack(expand="true", fill="both")
# Start the Tkinter event loop
root.mainloop()
Comments
Post a Comment