In today’s article, we are going to create a simple Google Translator application. Users will be able to input text to translate, choose the source language, and select the target language for the translation thanks to the tkinter
library.
Let’s get started!
Table of Contents
- Necessary Libraries
- Imports
- Translation Function
- Creating the Main Window
- Source Language Combobox
- Target Language Combobox
- Input Text Widget with Scrollbar
- Output Text Widget with Scrollbar
- Translate Button
- Adding Image and Icon
- Main Loop
- Example
- Full Code
Necessary Libraries
For the code to function properly, make sure to install these libraries via the terminal or your command prompt:
$ pip install tk
$ pip install googletrans==4.0.0-rc1
This allows you to use the tkinter and googletrans libraries.
Learn also:
- How to Translate Languages with Flask in Python
- How to Build a Language Translator with Transformers in Python
Imports
from tkinter import *
from tkinter import ttk
import googletrans
from googletrans import Translator
Let’s begin by bringing in tkinter
, our go-to tool for crafting a graphical user interface (GUI). To give our interface a polished look, we’ll also grab ttk
from tkinter
, which unlocks access to those sleek, themed widgets. And of course, we’ll be using the googletrans
library to tap into the power of Google Translate.
Translation Function
def translate_text():
source_lang = source_lang_combo.get()
target_lang = target_lang_combo.get()
input_text = input_text_widget.get("1.0","end-1c")
if not input_text:
output_text_widget.delete("1.0","end")
else:
translator = Translator()
translated_text = translator.translate(input_text,src=source_lang, dest=target_lang)
output_text_widget.delete("1.0","end")
output_text_widget.insert("1.0",translated_text.text)
Next, we define a function that verifies if the input text is empty or not, if it is empty then it ensures that the output widget is cleared. However, in the case that the input text is not empty then it translates the text and displays the translation in the output widget after extracting the source and target language as well as the input text thanks to the googletrans
library.
Creating the Main Window
#Create the main window
root = Tk()
root.title("Google Translator - The Pycodes")
root.geometry("1080x400")
root.resizable(False,False)
Then, we create the main window, give it a title, and define its geometry and background color, and disable its resizing.
Source Language Combobox
#source language combobox
source_lang_combo = ttk.Combobox(root,values=list(googletrans.LANGUAGES.values()),font="Roboto 14")
source_lang_combo.place(x=110,y=20)
source_lang_combo.set("English")
Following that, we create a combobox
where the user can select the source language by adding language options (from the googletrans
library); In this script, we have set the default source language as English.
Target Language Combobox
#target language combobox
target_lang_combo = ttk.Combobox(root,values=list(googletrans.LANGUAGES.values()),font="Roboto 14")
target_lang_combo.place(x=730,y=20)
target_lang_combo.set("Arabic")
What we did here is similar to the source language combobox
, with the difference being that this combobox
displays the language that the user wants to translate to, and the default target language is Arabic.
Input Text Widget with Scrollbar
#input text widget with scrollbar
input_text_frame = Frame(root, bd=5)
input_text_frame.place(x=10,y=118,width=440,height=210)
input_text_widget = Text(input_text_frame,font="Roboto 20", bg="white",relief=GROOVE, wrap=WORD)
input_text_widget.place(x=0,y=0,width=430,height=200)
input_text_scrollbar = ttk.Scrollbar(input_text_frame,orient="vertical", command=input_text_widget.yview)
input_text_scrollbar.pack(side="right",fill="y")
input_text_scrollbar.configure(command=input_text_widget.yview)
input_text_widget.configure(yscrollcommand=input_text_scrollbar.set)
After that, we create a frame to contain our multi-line input text widget, where the user can write their text. Then, we add a scrollbar to the input text widget for easy navigation.
Output Text Widget with Scrollbar
#output text widget with scrollbar
output_text_frame = Frame(root,bd=5)
output_text_frame.place(x=620, y=118, width=440, height=210)
output_text_widget = Text(output_text_frame,font="Roboto 20", bg="white",relief=GROOVE, wrap=WORD)
output_text_widget.place(x=0,y=0,width=430,height=200)
output_text_scrollbar = ttk.Scrollbar(output_text_frame,orient="vertical", command=output_text_widget.yview)
output_text_scrollbar.pack(side="right",fill="y")
output_text_scrollbar.configure(command=output_text_widget.yview)
output_text_widget.configure(yscrollcommand=output_text_scrollbar.set)
What we did here is similar to the previous part (input text widget with scrollbar). However, this frame will display the translated text.
Translate Button
#translate button
translate_button = Button(root,text="Translate",font="Roboto 15 italic",activebackground="green",
cursor="hand2",bd=5, bg="orange",fg="white",command=translate_text)
translate_button.place(x=475,y=250)
Here, we created a button called “Translate” that triggers the translate_text
function.
Adding Image and Icon
#adding image and icon
opposite_image = PhotoImage(file="opposite.png")
image_label = Label(root,image=opposite_image,width=150)
image_label.place(x=450,y=20)
image_icon = PhotoImage(file="translation.png")
root.iconphoto(False,image_icon)
Now, we add the opposite arrow image, which is located in the same file as this script, between the entry widgets. Additionally, we set the main window icon using the translation image from the same file. Overall, this part is optional in the script and serves purely aesthetic purposes.
Main Loop
root.mainloop()
Lastly, this part ensures that the main window is running and responsive to the user.
Example
Used Images:
Output:
Full Code
from tkinter import *
from tkinter import ttk
import googletrans
from googletrans import Translator
def translate_text():
source_lang = source_lang_combo.get()
target_lang = target_lang_combo.get()
input_text = input_text_widget.get("1.0","end-1c")
if not input_text:
output_text_widget.delete("1.0","end")
else:
translator = Translator()
translated_text = translator.translate(input_text,src=source_lang, dest=target_lang)
output_text_widget.delete("1.0","end")
output_text_widget.insert("1.0",translated_text.text)
#Create the main window
root = Tk()
root.title("Google Translator - The Pycodes")
root.geometry("1080x400")
root.resizable(False,False)
#source language combobox
source_lang_combo = ttk.Combobox(root,values=list(googletrans.LANGUAGES.values()),font="Roboto 14")
source_lang_combo.place(x=110,y=20)
source_lang_combo.set("English")
#target language combobox
target_lang_combo = ttk.Combobox(root,values=list(googletrans.LANGUAGES.values()),font="Roboto 14")
target_lang_combo.place(x=730,y=20)
target_lang_combo.set("Arabic")
#input text widget with scrollbar
input_text_frame = Frame(root, bd=5)
input_text_frame.place(x=10,y=118,width=440,height=210)
input_text_widget = Text(input_text_frame,font="Roboto 20", bg="white",relief=GROOVE, wrap=WORD)
input_text_widget.place(x=0,y=0,width=430,height=200)
input_text_scrollbar = ttk.Scrollbar(input_text_frame,orient="vertical", command=input_text_widget.yview)
input_text_scrollbar.pack(side="right",fill="y")
input_text_scrollbar.configure(command=input_text_widget.yview)
input_text_widget.configure(yscrollcommand=input_text_scrollbar.set)
#output text widget with scrollbar
output_text_frame = Frame(root,bd=5)
output_text_frame.place(x=620, y=118, width=440, height=210)
output_text_widget = Text(output_text_frame,font="Roboto 20", bg="white",relief=GROOVE, wrap=WORD)
output_text_widget.place(x=0,y=0,width=430,height=200)
output_text_scrollbar = ttk.Scrollbar(output_text_frame,orient="vertical", command=output_text_widget.yview)
output_text_scrollbar.pack(side="right",fill="y")
output_text_scrollbar.configure(command=output_text_widget.yview)
output_text_widget.configure(yscrollcommand=output_text_scrollbar.set)
#translate button
translate_button = Button(root,text="Translate",font="Roboto 15 italic",activebackground="green",
cursor="hand2",bd=5, bg="orange",fg="white",command=translate_text)
translate_button.place(x=475,y=250)
#adding image and icon
opposite_image = PhotoImage(file="opposite.png")
image_label = Label(root,image=opposite_image,width=150)
image_label.place(x=450,y=20)
image_icon = PhotoImage(file="translation.png")
root.iconphoto(False,image_icon)
root.mainloop()
Happy Coding!