Python Project - Text Editor with python and Tkinter

Python text editor is a standalone deployable application that is used as an alternative to Notepad. It uses python and the Tkinter python GUI library to provide one with the basic tools and functions needed to keep a record.


python project text editor


Setting up the necessary dependencies

Text editor or notepad works on python 3 and thus it requires python 3 installed. For installing python 3, visit official python website and download the latest stable release and proceed further.

Once python 3 is installed and the path is set up in the environment variables, we can verify the installation by running python -V in the command line. Successful installing would show a version number while unsuccessful installation will trigger an error.

Before getting started it is important to note that the python version running is 3.x. and not python2. The project is built on python3 and hence the code won’t work with the python2.

The next library required for Python Text editor project to work is the Tkinter library. To install Tkinter, open up the command line and type the following command:

pip install tk

Start the development of text editor project in Python


Download Python Text Editor Source Code


Before proceeding ahead, please download the source code of Python Text Editor Project: Text Editor Project in Python

1. Import dependencies

from tkinter import *
import tkinter.scrolledtext as ScrolledText
import tkinter.filedialog as FileDialog
import tkinter.messagebox as MessageBox
import tkinter.simpledialog as SimpleDialog
import os
from tkinter import font

2. Set up working window

#Creating base for the main working window
base = Tk(className = ' TechnologyMania Text Editor')
textArea = ScrolledText.ScrolledText(base, width = 160, height = 50)

3. Code for menu bar

#Creating cascade menu and menu options
menu = Menu(base)
base.config(menu = menu)

fileMenu = Menu(menu)
menu.add_cascade(label = "File", menu = fileMenu)
fileMenu.add_command(label = 'New', command = newFile)
fileMenu.add_command(label = 'Open', command = openFile)
fileMenu.add_command(label = 'Save', command = saveFile)
fileMenu.add_separator()
fileMenu.add_command(label = "Exit", command = exiteditor)

editmenu = Menu(menu)
menu.add_cascade(label = 'Edit', menu = editmenu)
editmenu.add_command(label = 'Change Font', command = changeFont)

textArea.pack()

#Making the window remain open
base.mainloop()

4. New file option

#New File Function
def newFile():

    #if content present
    if len(textArea.get('1.0' , END)) > 0:
        if MessageBox.askyesno("Save?" , "Do you wish to save the file"):
            saveFile()
            textArea.delete('1.0', END)
            base.title("TechnologyMania Text Editor")

        else:
            textArea.delete('1.0', END)
            base.title("TechnologyMania Text Editor")

5. Open file option

#Open File Function
def openFile():

    file = FileDialog.askopenfile(parent = base, title = 'Select a file', filetype = (("Text file" , "*.txt"),("All files" , "*.*")))

    base.title(os.path.basename(file.name) + "  - TechnologyMania Text Editor")

    if file != None:
        contents = file.read()
        textArea.delete('1.0', END)
        textArea.insert('1.0', contents)
        file.close()

6. Save file option

#Save File Function
def saveFile():

    file = FileDialog.asksaveasfile(mode = 'w', defaultextension = ".txt", filetype = (("Text file" , "*.txt"),("All files" , "*.*")))

    if file != None:
        data = textArea.get('1.0' , END)
        file.write(data)
        base.title(os.path.basename(file.name) + "  - TechnologyMania Text Editor")
        file.close()

7. Exit Option

#Exit function        
def exiteditor():

    if len(textArea.get('1.0' , END+'-1c')) > 0:
        
        if MessageBox.askyesno("Save?" , "Do you wish to save the file before closing"):
            saveFile()
            base.destroy()

        else:
            base.destroy()
    
    else:

        if MessageBox.askyesno("Quit", "Are you sure you want to quit ?"):
            base.destroy()

8. Change Font Option

def changeFont():

    #creating new window
    fontbase = Tk(className = 'Choose your Font')
    fontbase.minsize(height = 600, width = 600)

    #defining font selection dropdown and label
    font_label = Label(fontbase, text = "Choose Font", font = ("Helvetica", 14))
    font_options = list(font.families())
    font_options.sort()
    font_variable = StringVar(fontbase)
    font_variable.set(font_options[0])
    font_menu = OptionMenu(fontbase, font_variable, *font_options)
    font_menu.pack()
    font_menu.place(x = 50, y = 100)
    font_label.place(x = 35, y = 70)

    #defining style selection dropdown and label
    style_label = Label(fontbase, text = "Choose Style", font = ("Helvetica", 14))
    style_options = ['Normal', 'Bold', "Italic" , "Bold and Italic"]
    style_variable = StringVar(fontbase)
    style_variable.set(style_options[0])
    style_menu = OptionMenu(fontbase, style_variable, *style_options)
    style_menu.pack()
    style_menu.place(x = 250 , y = 100)
    style_label.place(x = 230, y = 70)

    #defining size selection dropdown and label
    size_label = Label(fontbase, text = "Choose Size", font = ("Helvetica", 14))
    size_options = ['6','8','10','12','14','16','20','24','30','32','36','40','46','52','60','72','80']
    size_variable = StringVar(fontbase)
    size_variable.set('10')
    size_menu = OptionMenu(fontbase, size_variable, *size_options)
    size_menu.pack()
    size_menu.place(x = 450, y = 100)
    size_label.place(x = 425, y = 70)

    #confirm funtion
    def confirm():
        f_C = font_variable.get()
        s_C = style_variable.get()
        si_C = int(size_variable.get())

        if(s_C == 'Bold'):
            textArea.configure(font=("f_C", si_C, "bold"))

        if(s_C == 'Normal'):
            textArea.configure(font=("f_C", si_C, "normal"))

        if(s_C == 'Italic'):
            textArea.configure(font=("f_C", si_C, "italic"))

        if(s_C == "Bold and Italic"):
            textArea.configure(font=("f_C", si_C, 'bold', "italic"))

        fontbase.destroy()

    #cancel function
    def cancel():
        fontbase.destroy()

    #creating and placing the buttons    
    confirm_btn = Button(fontbase, text = "Confirm", command = confirm)
    cancel_btn = Button(fontbase, text = "Cancel", command = cancel)    

    confirm_btn.place(x = 400, y = 530)
    cancel_btn.place(x = 500, y = 530)
    
    fontbase.mainloop()

Summary

In this python project, we created a text editor or notepad with Tkinter. This is a good python project for beginners to brush up python skills.

If you like this project, please rate Technology Mania on Facebook

No comments:

Post a Comment