How to create a simple text editor with tkinter

If we want to create a GUI program in Python 3 then Tkinter seems like the best choice because 1) tkinter module has already been included in Python 3 and thus we do not need to download and install an extra module 2) wxPython does not support Python 3 development and also it is already out of date.

Below is the python program I have created which allows user to input text into the textbox then saves it in the text file later on.

import tkinter as tk 
from tkinter import ttk 
from tkinter import Text

win = tk.Tk() #create instance
win.title("Write Note") #add a title

def saveFile():
        with open('note.txt', 'w') as note: #open file
            text = text_entry.get("1.0", 'end-1c') #get text entry
            note.write(text) #write text to file
    except IOError as err:
        print("File error " + str(err))
text_entry = Text(win,width=50,height=30) #create text editor
text_entry.grid(column=0, row=0) 

action = ttk.Button(win, text="Save", command=saveFile) #create button
action.grid(column=0, row=1)

win.mainloop() #start GUI

The program above will create a textbox and a button which once you have clicked on it it will call the saveFile() function to save the text into a text file (or creates the text file first if it has not been created yet).

Enter some words then press Save
Enter some words then press Save

tkinter module actually can match wxPython in every field and therefore there is no need for us to use another new module for our Python 3 GUI development!

Latest update :

I have decided to make a few changes to the script and come out with a new script to create our text editor. Kindly read the text editor latest development in this article