Learn How to Create Notepad in Python

Work on a simple (but interesting) Python Project – Create a Text Editor (Notepad) in Python.

This project will help you understand functional programming in python as we have implemented almost everything using separate functions. Features of python text editor:

1. Change font family
2. Bold
3. Save the document

Project Prerequisites

The prerequisites of python text editor project are as follows :

1. Basic concepts of Python
2. Tkinter

To install the libraries, you can use pip installer from the cmd/Terminal:

Pip install tkinter

Download Python Text Editor Code

Please download source code of python text editor – Text Editor Source Code

Creating main.py

Code:

from tkinter import *
from tkinter import font, filedialog

def saveDoc():
    global textarea
    text=textarea.get("1.0","end-1c")
    location=filedialog.asksaveasfilename()
    file=open(location,"w+")
    file.write(text)
    file.close()

def Algerian():
    global textarea
    textarea.config(font="Algerian")  

def Arial():
    global textarea
    textarea.config(font="Arial")  

def Courier():
    global textarea
    textarea.config(font="Courier")  

def Cambria():
    global textarea
    textarea.config(font="Cambria")

def boldDoc():
    global textarea
    textarea.config(font=('arial',14,'bold'))

root=Tk()
root.title("ProjectGurukul Text Editor Python")

savebtn=Button(root,command=saveDoc,text="Save")
savebtn.grid(row=1,column=0)
savebtn.config(font=('arial',20,'bold'),bg="DodgerBlue2",fg="white")

fontbtn=Menubutton(root,text="Font")
fontbtn.config(font=('arial',20,'bold'),bg="DodgerBlue2",fg="white")
fontbtn.grid(row=1,column=1)
fontbtn.menu=Menu(fontbtn,tearoff=0)
fontbtn["menu"]=fontbtn.menu

fontbtn.menu.add_checkbutton(label="Arial",
command=Arial)

fontbtn.menu.add_checkbutton(label="Algerian", 
command=Algerian)

fontbtn.menu.add_checkbutton(label="Cambria", 
command=Cambria)

fontbtn.menu.add_checkbutton(label="Courier",
command=Courier)

boldbtn=Button(root,command=boldDoc,text="Bold")
boldbtn.grid(row=1,column=2)
boldbtn.config(font=('arial',20,'bold'),bg="DodgerBlue2",fg="white")

textarea=Text(root)
textarea.grid(row=2,columnspan=5)

# To know or add more font options
# fonts=list(font.families())
# fonts.sort() 

mainloop()

Functions Used:

  1. Savedoc: It will ask for the location where the user wants to save the current document and then the file name. And finally, it will save the document.
  2. bolddoc: It will highlight the entire text of the document by changing its style to bold.
  3. Algerian: It will change the font style of the entire text to Algerian.
  4. Arial: It will change the font style of the entire text to Arial.
  5. Courier: It will change the font style of the entire text to Courier
  6. Cambria: It will change the font style of the entire text to Cambria.

Variables Used:

  • root: the main GUI window.
  • savebtn: it saves the document by calling savedoc function.
  • fontbtn: It is a menu that displays all available fonts. If you want to add more fonts you may add as many as you want, uncommenting the below lines will list all available fonts.

# To know or add more font options
# fonts=list(font.families())
# fonts.sort()

  • boldbtn: It calls bolddoc function which highlights the text.

Text Editor Output
python text editor

Summary

We have successfully developed python text editor (notepad) project. We have many such simple and interesting projects published on our ProjectGurukul, check them, and keep learning.

Leave a Reply

Your email address will not be published. Required fields are marked *