In this Python project, we will create a GUI-based Text Editor using only the Tkinter module in Python. It is a beginner-level project, and be able to use some amazing basic GUI components in real life.
About Text Editor:
A notepad is a text-only editor that only works with .txt files but can still read and edit file formats that can be edited. This project is a very simple one to make, but you are still going to have a lot of fun creating it.
Project Prerequisites:
To build this text editor in python, we will need the following libraries:
- Tkinter – To create the GUI.
- PIL (Python Image Library) – To give the GUI window an icon.
- OS – To get the path of the file.
python -m pip install pillow
Step 1: Importing Libraries
from tkinter import *
import tkinter.filedialog as fd
import tkinter.messagebox as mb
from PIL import Image, ImageTk
import os
Step 2: Initializing the GUI
root = Tk()
root.title("Untitled - Notepad")
root.geometry('800x500')
root.resizable(0, 0)
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
# icon = ImageTk.PhotoImage(Image.open('Notepad.png'))
# root.iconphoto(False, icon)
root.update()
root.mainloop()
Step 3: Defining Functions
We need functions for Opening, Saving, and Editing text. For example, the open_file function:
def open_file():
file = fd.askopenfilename(defaultextension='.txt', filetypes=[('All Files', '*.*'), ("Text File", "*.txt*")])
if file != '':
root.title(f"{os.path.basename(file)}")
text_area.delete(1.0, END)
with open(file, "r") as file_:
text_area.insert(1.0, file_.read())
file_.close()
Congratulations! You've just laid the foundation for your own Text Editor in Python.