Tuning up the previous text editor with tkinter

In this article I will continue with the text editor development tutorial. In the previous post I have created a text box and a save button for my text editor so we can save our work later on, however the previous interface does not look professional so I have decided to change and redo the entire GUI to make it looks better as well as prepared the text editor for the future expansion.

All right, without further talking let get started. I have broken down the python script into small parts to make it easy for you to follow my code.

1) Import a few modules into our new program.

import tkinter as tk 
from tkinter import scrolledtext
from tkinter import Menu
from tkinter import ttk

2) Create the window object.

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

3) Create the label frame to host the checkboxes

labelFrame = ttk.LabelFrame(win, text="Text Property")
labelFrame.grid(column=0, row=2, padx=30, pady=10, sticky=tk.W)

4) Create the checkboxes and hosted them in labelFrame.

#create a few checkboxes
ckValueObj1 = tk.IntVar()
ckValueObj2 = tk.IntVar()
ckValueObj3 = tk.IntVar()

chk1 = tk.Checkbutton(labelFrame, text="Highlight Text", variable=ckValueObj1, command=changeState)
chk1.deselect()
chk1.grid(column=0, row=2, sticky=tk.W)

chk2 = tk.Checkbutton(labelFrame, text="Bold Text", variable=ckValueObj2, command=changeState)
chk2.deselect()
chk2.grid(column=1, row=2, sticky=tk.W)

chk3 = tk.Checkbutton(labelFrame, text="Green Text", variable=ckValueObj3, command=changeState)
chk3.deselect()
chk3.grid(column=2, row=2, sticky=tk.W)

5) Create the function which will be called every time we checked or unchecked any checkbox.

def changeState(): # receive chackbox updates
    
    state1 = ckValueObj1.get() #use this value to highlight the background of the text with yellow color
    state2 = ckValueObj2.get() #use this value to change the text to bold
    state3 = ckValueObj3.get() #use this value to turn the text to green
    
    if state1 == 1: 
        scrollText.tag_add('highlightline', '1.0', 'end-1c')
        scrollText.tag_configure('highlightline', background='yellow', relief='raised')
    else:
        scrollText.tag_remove('highlightline', '1.0', 'end-1c')
        
    if state2 == 1:
        scrollText.tag_add('makebold', '1.0', 'end-1c')
        scrollText.tag_configure('makebold', font='helvetica 12 bold', relief='raised')
    else:
        scrollText.tag_remove('makebold', '1.0', 'end-1c')
    
    if state3 == 1:
        scrollText.tag_add('green', '1.0', 'end-1c')
        scrollText.tag_configure('green', foreground='green', relief='raised')
    else:
        scrollText.tag_remove('green', '1.0', 'end-1c')

6) Create the scroll text editor.

scrolW = 50
scrolH = 10 
scrollText = scrolledtext.ScrolledText(win, width=scrolW, height=scrolH, wrap=tk.WORD)
scrollText.grid(column=0, row= 0, columnspan=3)

7) Create the main menu items.

menuBar = Menu(win) # create menu bar
win.config(menu=menuBar) # attach menubar to window

fileMenu = Menu(menuBar, tearoff=0) # create a menu in the menu bar and remove the default dash line
fileMenu.add_command(label="Save", command=_saveFile) # create the save file sub menu
fileMenu.add_command(label="Exit", command=_closeFile) # create the close file sub menu
menuBar.add_cascade(label="File", menu=fileMenu) # create main menu

8) Create the save file function for the Save item.

def _saveFile(): #save file command
    try:
        with open('note.txt', 'w') as note:
            text = scrollText.get("1.0", 'end-1c') #get text entry
            note.write(text) #write text to file
    except IOError as err:
        print("File error " + str(err))

9) Create exit command for the Exit item.

def _closeFile(): #close file command
    win.quit()
    win.destroy()
    exit()

10) Most of the script above is self-explaining so you should understand them if you know the basic python language. The full code is as follow.

import tkinter as tk 
from tkinter import scrolledtext
from tkinter import Menu
from tkinter import ttk

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

def _saveFile(): #save file command
    try:
        with open('note.txt', 'w') as note:
            text = scrollText.get("1.0", 'end-1c') #get text entry
            note.write(text) #write text to file
    except IOError as err:
        print("File error " + str(err))
        
def _closeFile(): #close file command
    win.quit()
    win.destroy()
    exit()
    
def changeState(): # receive chackbox updates
    
    state1 = ckValueObj1.get() #use this value to highlight the background of the text with yellow color
    state2 = ckValueObj2.get() #use this value to change the text to bold
    state3 = ckValueObj3.get() #use this value to turn the text to green
    
    if state1 == 1: 
        scrollText.tag_add('highlightline', '1.0', 'end-1c')
        scrollText.tag_configure('highlightline', background='yellow', relief='raised')
    else:
        scrollText.tag_remove('highlightline', '1.0', 'end-1c')
        
    if state2 == 1:
        scrollText.tag_add('makebold', '1.0', 'end-1c')
        scrollText.tag_configure('makebold', font='helvetica 12 bold', relief='raised')
    else:
        scrollText.tag_remove('makebold', '1.0', 'end-1c')
    
    if state3 == 1:
        scrollText.tag_add('green', '1.0', 'end-1c')
        scrollText.tag_configure('green', foreground='green', relief='raised')
    else:
        scrollText.tag_remove('green', '1.0', 'end-1c')
 
#create label frame to host the checkboxes    
labelFrame = ttk.LabelFrame(win, text="Text Property")
labelFrame.grid(column=0, row=2, padx=30, pady=10, sticky=tk.W)

#create a few checkboxes
ckValueObj1 = tk.IntVar()
ckValueObj2 = tk.IntVar()
ckValueObj3 = tk.IntVar()

chk1 = tk.Checkbutton(labelFrame, text="Highlight Text", variable=ckValueObj1, command=changeState)
chk1.deselect()
chk1.grid(column=0, row=2, sticky=tk.W)

chk2 = tk.Checkbutton(labelFrame, text="Bold Text", variable=ckValueObj2, command=changeState)
chk2.deselect()
chk2.grid(column=1, row=2, sticky=tk.W)

chk3 = tk.Checkbutton(labelFrame, text="Green Text", variable=ckValueObj3, command=changeState)
chk3.deselect()
chk3.grid(column=2, row=2, sticky=tk.W)

# create a scroll text editor
scrolW = 50
scrolH = 10 
scrollText = scrolledtext.ScrolledText(win, width=scrolW, height=scrolH, wrap=tk.WORD)
scrollText.grid(column=0, row= 0, columnspan=3)

# create menu items
menuBar = Menu(win) # create menu bar
win.config(menu=menuBar) # attach menubar to window

fileMenu = Menu(menuBar, tearoff=0) # create a menu in the menu bar and remove the default dash line
fileMenu.add_command(label="Save", command=_saveFile) # create the save file sub menu
fileMenu.add_command(label="Exit", command=_closeFile) # create the close file sub menu
menuBar.add_cascade(label="File", menu=fileMenu) # create main menu

scrollText.focus() #focus on the scroll text editor

win.mainloop() #start GUI

If you run the program now in NetBeans 8.1 IDE then you can check on the checkboxes below the scroll text editor to see various effects that we have included in our python script above.

Text Editor
Text Editor

You can select the save command to save your file in plain text or the exit command to terminate the program under the File’s drop down menu. I will continue to develop this text editor so make sure you return and read more about it, you can find this text editor under the python text editor tag on this blog.

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():
    try:
        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)
action.config(width=66)

text_entry.focus()
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 http://gamingdirectional.com/blog/2016/07/30/tuning-up-the-previous-text-editor-with-tkinter/