Build Your First Transliterator Text Editor with Python Tkinter

Merin Rose Tom
4 min readJan 16, 2022

Let’s create your first text editor in python tkinter which will convert the texts from English to phonetic equivalent in another language.

Photo by Nick Morrison on Unsplash

Have you ever faced a situation where you want to create some content in some local language but the slow typing speed in the language creates a problem? Then this article will probably help you in it if you are good in typing English.

We will be using a concept called transliteration to increase the typing speed in the regional language and tkinter will be used to create the text editor.

What is Transliteration ?

Transliteration is basically conversion of text from one language to another. Ohh … So it is simply translation right? No!

Transliteration converts the word from one language to its phonetic equivalent in another language. Imagine that your mother tongue is not English and you want to type something in your mother tongue but the keyboard available is English keyboard. So you use the English alphabets but actually you are using your mother tongue! That is Transliteration…

What is Tkinter ?

Tkinter is a python gui framework which can be used for simple gui applications such as

  • Text editor
  • Temperator convertor
  • Todo List

The main advantage of tkinter is its simplicity and cross platform usefulness. Although it wont be useful for complex gui creation you can write applications which are not frontend intensive in tkinter.

What all you need to start?

As this is a simple application you only need the following items to get started.

  • Python : Python should be installed in your system as we are using python to write the code. Python3 is preferred along with pip.
  • Tkinter : The below mentioned command can be used if you are using linux.
pip install tk
  • Indic Transliteration : This is the package that we will be using for transliteration. Detailed documentation can be found here.
pip install indic-transliteration

Let’s Start the Coding!

So first lets create a simple text box in tkinter which will accept texts in English.

First we will inherit the tkinter class and add a text editor . Then we will create an object of our class called myapp. myapp.mainloop() will open the tkinter gui.

# main.py
from tkinter import Tk, TOP, X, StringVar, Text, BOTH, END
from tkinter.ttk import Label, Combobox
class MyApp(Tk):
"""
Inherits the tkinter class to modify its functionalities.
"""
def __init__(self):
# Adding initial parameters
super().__init__()
self.geometry('1200x800')
self.title("Indic Text Editor")
self.cur_lang = "Malayalam"

# ------------------ text editor ---------------------------

self.text_editor = Text(self)
self.text_editor.focus_set()
# Setting cursor at the text box beginning
self.text_editor.pack(fill=BOTH, expand=True)
# To display the text editor

# ------------------ end text editor -----------------------
if __name__ == "__main__":
print("Running the text editor")
myapp = MyApp()
myapp.mainloop()

If you run the above code using python main.py you can see a window where you can write any text .

Now let’s add the transliterator functionality. We need to create the following functionalities

  • Toolbox : The toolbox will contain a drop down with possible languages so that user can select language of their choice. For that the following code needs to be added to the end of init method of our MyApp class
# ----------------- tool box ---------------------------------

tool_bar = Label(self) # The box where the language dropdown will be added
tool_bar.pack(side=TOP, fill=X) # To display the tool box in the top filling the x axis

# Language Box
lang_tuple = list(LANG_LIST.keys())
self.lang_selected = StringVar()
# textvariable will store the user selected option
lang_box = Combobox(tool_bar, width=20, textvariable=self.lang_selected, state="readonly")
lang_box["values"] = lang_tuple # Dropdown options for the language
lang_box.current(lang_tuple.index(self.cur_lang)) # The language initially displayed
lang_box.grid(row=0, column=9, padx=2) # Positioning the combo box

# ----------------- end tool box ---------------------------------
  • Binders and event methods : Binders bind an event like a key press to a method or action. When the user press space or enter key after typing a word we will convert the word. For that event methods will be used. Add the following binders to the end of init method.
# ------------------ events -------------------------------
lang_box.bind("<<ComboboxSelected>>", self.change_language)
self.text_editor.bind("<space>", self.translate)
self.text_editor.bind("<Return>", self.translate)
# ------------------ end events -------------------------------

Now we will add the event methods which will be triggered on the above events happen as a member method of the class MyApp.

# ----------- event driven methods --------------

def change_language(self, event=None):
self.cur_lang = self.lang_selected.get() # Changing the language

def translate(self, event):
"""
The method translates phonetic English text to another language using trans-iterate method.
"""
final_result = ''
sub_string = ''
word = str(self.text_editor.get(0.0, END)).rstrip("\n").split(" ")[-1].split('\n')[-1] # Getting word
current_mal_text = str(self.text_editor.get(0.0, END)).rstrip('\n').rstrip(word) # The old string
# Transliterating word avoiding non-alphabetic characters
for element in word:
if element.isalpha():
sub_string += element
else:
result = transliterate(sub_string, sanscript.ITRANS, LANG_LIST[self.cur_lang]) if sub_string else ''
final_result += result + element
sub_string = ''
if sub_string:
final_result += transliterate(sub_string, sanscript.ITRANS, LANG_LIST[self.cur_lang]) if sub_string else ''
self.text_editor.delete(1.0, END) # Removing the current content on text editor
self.text_editor.insert(0.0, current_mal_text) # Adding the old string
self.text_editor.insert(END, final_result) # Attaching result to old string

# ----------- end event driven methods ----------

And thats it!! Run the program using the below command assuming that your file name is main.py.

python main.py

Your text editor will look something like this…

The complete code for this can be found here.

Languages Supported

1. Devanagari
2. Gujarati
3. Gurmukhi
4. Gondi_Gunjala
5. Bengali
6. Oriya
7. Kannada
8. Malayalam
9. Tamil
10. Grantha
11. Telugu

Next Steps…

We can add additional functionalities like menu bars, text formatting options, file operations etc.

Happy coding …

--

--

Merin Rose Tom

Software engineer || Python programmer || Full stack developer