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.