Creating a User-Friendly Language Converter in Python

Faraz

By Faraz - September 18, 2023

Learn how to build a Python translator using tkinter for the GUI and googletrans API for language translation. A beginner-friendly tutorial.


Create a Python Translator Using tkinter and googletrans.jpg

Are you ready to embark on a journey into the world of Python programming and language translation? In this article, we'll guide you through the process of creating a Python Translator using tkinter and the powerful googletrans library. Buckle up, as we dive into the fascinating world of coding and translation!

Table of Contents

  1. Understanding the Basics
  2. Setting Up Your Development Environment
  3. Importing the Libraries
  4. Full Source Code
  5. Explanation of Source Code
  6. Conclusion
  7. FAQs

1. Understanding the Basics

What is tkinter?

Tkinter is a standard Python interface to the Tk GUI toolkit. It is widely used for creating graphical user interfaces (GUIs) for desktop applications. Tkinter provides a set of tools and widgets that allow developers to design and build interactive applications with ease.

What is googletrans?

Googletrans is a Python library that acts as a wrapper for Google Translate. It enables developers to access Google's powerful translation engine programmatically. With googletrans, you can easily translate text from one language to another, making it an excellent choice for our translator project.

2. Setting Up Your Development Environment

Before we dive into the code, let's make sure you have everything you need to get started:

  1. Python Installation: Ensure you have Python installed on your computer. You can download the latest version from the official Python website.
  2. Install tkinter: Tkinter is included with most Python installations, so you typically don't need to install it separately.
  3. Install googletrans: You can install googletrans using pip, the Python package manager, by running the following command in your terminal or command prompt:
    pip install googletrans==4.0.0-rc1

3. Importing the Libraries

Here's a snippet to get you started:

from tkinter import *
import tkinter as tk
from tkinter import ttk
from googletrans import Translator
from tkinter import messagebox

4. Full Source Code

from tkinter import *
import tkinter as tk
from tkinter import ttk
from googletrans import Translator
from tkinter import messagebox

# Creating Tkinter Scaffold
root = tk.Tk()
root.title('Langauge Translator')
root.geometry('530x330')
root.maxsize(530, 330)
root.minsize(530, 330)

# Function to translate using the translator package


def translate():
    language_1 = t1.get("1.0", "end-1c")
    cl = choose_langauge.get()

    if language_1 == '':
        messagebox.showerror('Language Translator', 'please fill the box')
    else:
        t2.delete(1.0, 'end')
        translator = Translator()
        output = translator.translate(language_1, dest=cl)
        t2.insert('end', output.text)


# Function to clear the input fields
def clear():
    t1.delete(1.0, 'end')
    t2.delete(1.0, 'end')


# SelectBox 1 for auto detected language
auto_detect_language = tk.StringVar()
auto_detect = ttk.Combobox(
    root,
    width=20,
    textvariable=auto_detect_language,
    state='readonly',
    font=('verdana', 10, 'bold'),
)

auto_detect['values'] = ('Auto Detect', )

auto_detect.place(x=30, y=70)
auto_detect.current(0)

# SelectBox 2 for selected language
language_selected = tk.StringVar()
choose_langauge = ttk.Combobox(root,
                               width=20,
                               textvariable=language_selected,
                               state='readonly',
                               font=('verdana', 10, 'bold'))

# List of available language options for translation
choose_langauge['values'] = (
'Afrikaans', 'Albanian', 'Arabic', 'Armenian', ' Azerbaijani', 'Basque', 'Belarusian', 'Bengali', 'Bosnian', 'Bulgarian', ' Catalan', 'Cebuano', 'Chichewa', 'Chinese', 'Corsican', 'Croatian', ' Czech', 'Danish', 'Dutch', 'English', 'Esperanto', 'Estonian', 'Filipino', 'Finnish', 'French', 'Frisian', 'Galician', 'Georgian', 'German', 'Greek', 'Gujarati', 'Haitian Creole', 'Hausa', 'Hawaiian', 'Hebrew', 'Hindi', 'Hmong', 'Hungarian', 'Icelandic', 'Igbo', 'Indonesian', 'Irish', 'Italian', 'Japanese', 'Javanese', 'Kannada', 'Kazakh', 'Khmer', 'Kinyarwanda', 'Korean', 'Kurdish', 'Kyrgyz', 'Lao', 'Latin', 'Latvian', 'Lithuanian', 'Luxembourgish', 'Macedonian', 'Malagasy', 'Malay', 'Malayalam', 'Maltese', 'Maori', 'Marathi', 'Mongolian', 'Myanmar', 'Nepali', 'Norwegian' 'Odia', 'Pashto', 'Persian', 'Polish', 'Portuguese', 'Punjabi', 'Romanian', 'Russian', 'Samoan', 'Scots Gaelic', 'Serbian', 'Sesotho', 'Shona', 'Sindhi', 'Sinhala', 'Slovak', 'Slovenian', 'Somali', 'Spanish', 'Sundanese', 'Swahili', 'Swedish', 'Tajik', 'Tamil', 'Tatar', 'Telugu', 'Thai', 'Turkish', 'Turkmen', 'Ukrainian', 'Urdu', 'Uyghur', 'Uzbek', 'Vietnamese', 'Welsh', 'Xhosa' 'Yiddish', 'Yoruba', 'Zulu',
)

choose_langauge.place(x=290, y=70)
choose_langauge.current(0)

# To store Input Text
t1 = Text(root, width=30, height=10, borderwidth=5, relief=RIDGE)
t1.place(x=10, y=100)

# To store translated Text
t2 = Text(root, width=30, height=10, borderwidth=5, relief=RIDGE)
t2.place(x=260, y=100)

button = Button(root,
                text="Translate",
                relief=RIDGE,
                borderwidth=3,
                font=('verdana', 10, 'bold'),
                cursor="hand2",
                foreground='Green',
                command=translate)
button.place(x=150, y=280)

clear = Button(root,
               text="Clear",
               relief=RIDGE,
               borderwidth=3,
               font=('verdana', 10, 'bold'),
               cursor="hand2",
               foreground='Red',
               command=clear)
clear.place(x=280, y=280)

root.mainloop()

5. Explanation of Source Code

This Python code uses the Tkinter library to create a simple graphical user interface (GUI) application for translating text from one language to another. Let's break down the code step by step:

Importing necessary libraries:

  • from tkinter import *: Imports all the classes and functions from the tkinter library.
  • import tkinter as tk: Imports the tkinter library with the alias tk.
  • from tkinter import ttk: Imports the ttk module from the tkinter library.
  • from googletrans import Translator: Imports the Translator class from the googletrans library for language translation.
  • from tkinter import messagebox: Imports the messagebox module from tkinter for displaying message boxes.

Creating the Tkinter window:

  • root = tk.Tk(): Creates the main window for the application.
  • root.title('Language Translator'): Sets the title of the window.
  • root.geometry('530x330'): Sets the initial size of the window.
  • root.maxsize(530, 330): Sets the maximum window size.
  • root.minsize(530, 330): Sets the minimum window size.

Defining functions:

  • translate(): This function is called when the "Translate" button is pressed. It retrieves the text entered in the input field (t1), detects the selected language, and translates the text using the googletrans library. The translated text is then displayed in the output field (t2).
  • clear(): This function is called when the "Clear" button is pressed. It clears the text in both the input and output fields.

Creating ComboBoxes:

  • auto_detect: A ComboBox (ttk.Combobox) for selecting the source language. In this case, it only contains one option, "Auto Detect."
  • choose_langauge: A ComboBox for selecting the target language. It contains a long list of available languages for translation.

Input and Output Text Fields:

  • t1: A Text widget for entering the text to be translated.
  • t2: A Text widget for displaying the translated text.

Create Translate and Clear Buttons:

  • button: A "Translate" button that calls the translate() function when clicked.
  • clear: A "Clear" button that calls the clear() function when clicked.

Placing GUI elements:

  • Various place() method calls are used to position the ComboBoxes, Text widgets, and buttons within the Tkinter window.

Starting the Tkinter main loop:

  • root.mainloop(): Enters the main event loop, which listens for user interactions and keeps the GUI application running.

6. Conclusion

In just a few steps, we've created a Python Translator using tkinter and googletrans. This powerful tool can help bridge language barriers and make communication across languages a breeze. Whether you're a language enthusiast or need translation for practical purposes, this project is an excellent addition to your Python skills repertoire.

So, what are you waiting for? Dive into the world of Python programming and start translating with ease!

7. FAQs

1. Can I use googletrans for commercial applications?

Yes, you can use googletrans for commercial applications as long as you comply with Google's terms of service and any relevant legal requirements.

2. How accurate is the translation provided by googletrans?

Googletrans uses Google's translation engine, which is known for its accuracy. However, like all machine translation tools, it may not always provide perfect translations, especially for complex or context-dependent text.

3. Can I customize the tkinter interface further?

Absolutely! You can customize the tkinter interface to suit your preferences by adding more widgets, changing colors, and adjusting the layout.

4. Is there a limit to the number of translations I can make with googletrans?

Googletrans doesn't impose a strict limit on the number of translations you can make, but it's always a good idea to review Google's usage policies to ensure compliance.

5. How do I install tkinter and googletrans?

To install tkinter, you don't need to do anything extra since it's included with Python. For googletrans, use the following pip command:

pip install googletrans==4.0.0-rc1

6. Where can I find more tkinter and googletrans documentation?

You can find comprehensive documentation for tkinter on the official Python website. For googletrans, you can refer to the documentation on the library's GitHub repository.

That’s a wrap!

I hope you enjoyed this article

Did you like it? Let me know in the comments below 🔥 and you can support me by buying me a coffee.

And don’t forget to sign up to our email newsletter so you can get useful content like this sent right to your inbox!

Thanks!
Faraz 😊

End of the article

Subscribe to my Newsletter

Get the latest posts delivered right to your inbox


Latest Post