In this article, we’ll walk through creating a real-time translation application in Python. Using the Deep Translator library as the translation engine and Tkinter for the graphical user interface (GUI), we can build a simple yet powerful app.
Below is a step by step implementation of the app:
Step 1: Install Necessary Libraries
We need two libraries:
pip install deep-translator
pip install tk
- deep-translator: Provides access to multiple translation engines including Google Translate.
- tkinter: Python’s built-in library for GUI development.
Step 2: Create app.py and import required libraries
This app will have a single app.py file which will contain all the code. Add the code below to import the required libraries to build the application:
from tkinter import *
from tkinter import ttk
from deep_translator import GoogleTranslator
- tkinter: Builds the GUI.
- ttk: Adds styled widgets like the dropdown menu.
- GoogleTranslator: Handles translation.
Step 3: Setting up the Tkinter Window
Here, the code create a Tkinter window ('root') with dimensions 1100x320, set it to be non-resizable, give it a pink background, and assign the title 'Real-time translator'.
root = Tk()
root.geometry('1100x320')
root.resizable(0, 0)
root['bg'] = 'pink'
root.title('Real-time Translator')
Step 4: Creating the GUI
We add labels, input fields, output text area, and a dropdown for selecting the target language.
Label(root, text="Language Translator", font="Arial 20 bold").pack()
Label(root, text="Enter Text", font='arial 13 bold', bg='white smoke').place(x=165, y=90)
Input_text = Entry(root, width=60)
Input_text.place(x=30, y=130)
Label(root, text="Output", font='arial 13 bold', bg='white smoke').place(x=780, y=90)
Output_text = Text(root, font='arial 10', height=5, wrap=WORD, padx=5, pady=5, width=50)
Output_text.place(x=600, y=130)
# Supported languages
languages = ['english', 'french', 'german', 'spanish', 'hindi', 'italian', 'japanese']
dest_lang = ttk.Combobox(root, values=languages, width=22)
dest_lang.place(x=130, y=180)
dest_lang.set('Choose Language')
Step 5: Translation Function
def Translate():
try:
translation = GoogleTranslator(source='auto', target=dest_lang.get()).translate(Input_text.get())
Output_text.delete(1.0, END)
Output_text.insert(END, translation)
except Exception as e:
print(f"Translation error: {e}")
- Detects source language automatically.
- Translates to the selected target language.
- Displays the result in the output box.
Step 6: Button for Translation
This code creates a Tkinter button for translation with the label 'Translate', styled in Arial font. It triggers the 'Translate' function on click, with an orange background and green active background, positioned at (445, 180) in the window.
trans_btn = Button(root, text='Translate', font='arial 12 bold', pady=5,
command=Translate, bg='orange', activebackground='green')
trans_btn.place(x=445, y=180)
Step 7: Run Tkinter main loop
This keeps the window running until the user closes it.
root.mainloop()
Complete Code
from tkinter import *
from tkinter import ttk
from deep_translator import GoogleTranslator
# Create main window
root = Tk()
root.geometry('1100x320')
root.resizable(0, 0)
root['bg'] = 'pink'
root.title('Real-time Translator')
# Labels and input/output fields
Label(root, text="Language Translator", font="Arial 20 bold").pack()
Label(root, text="Enter Text", font='arial 13 bold', bg='white smoke').place(x=165, y=90)
Input_text = Entry(root, width=60)
Input_text.place(x=30, y=130)
Label(root, text="Output", font='arial 13 bold', bg='white smoke').place(x=780, y=90)
Output_text = Text(root, font='arial 10', height=5, wrap=WORD, padx=5, pady=5, width=50)
Output_text.place(x=600, y=130)
# Supported languages
languages = ['english', 'french', 'german', 'spanish', 'hindi', 'italian', 'japanese']
dest_lang = ttk.Combobox(root, values=languages, width=22)
dest_lang.place(x=130, y=180)
dest_lang.set('Choose Language')
# Translation function
def Translate():
try:
translation = GoogleTranslator(source='auto', target=dest_lang.get()).translate(Input_text.get())
Output_text.delete(1.0, END)
Output_text.insert(END, translation)
except Exception as e:
print(f"Translation error: {e}")
# Translate button
trans_btn = Button(root, text='Translate', font='arial 12 bold', pady=5,
command=Translate, bg='orange', activebackground='green')
trans_btn.place(x=445, y=180)
# Run the app
root.mainloop()
Output: