In this lesson i want to show you How to Build Simple Translator with Python , Building a simple translator with Python involves several steps:
- Import the necessary libraries: You will need to import the
googletrans
library for translation, and thetkinter
library for creating the GUI.
1 2 |
from googletrans import Translator import tkinter as tk |
- Create the main window: You can create the main window of the application using the
Tk()
function. You can set the title, size, and other properties of the window.
1 2 3 |
root = tk.Tk() root.title("Translator") root.geometry("300x200") |
- Create the input and output widgets: You can create input and output widgets such as
Entry
andLabel
to receive the text to translate and display the translated text. You can also create aCombobox
widget to allow users to select the target language.
1 2 3 4 5 6 7 8 9 |
input_text = tk.Entry(root) input_text.pack() output_text = tk.Label(root) output_text.pack() languages = tk.Combobox(root, values=["en", "fr", "de", "es", "it"]) languages.set("Select a Language") languages.pack() |
- Create the translate function: You can create a function that will take the input text, target language, and use the
Translator
class from thegoogletrans
library to translate the text. The function should then update the output label with the translated text.
1 2 3 4 |
def translate(): input_text_value = input_text.get() translator = Translator() output_text.config(text=translator.translate(input_text_value, dest=languages.get()).text) |
- Create the translate button: You can create a button that when clicked, it calls the translate function.
1 2 |
translate_button = tk.Button(root, text="Translate", command=translate) translate_button.pack() |
- Run the application: You can run the application by calling the
mainloop()
function.
1 |
root.mainloop() |
This is the complete code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
from googletrans import Translator import tkinter as tk root = tk.Tk() root.title("Translator") root.geometry("300x200") input_text = tk.Entry(root) input_text.pack() output_text = tk.Label(root) output_text.pack() languages = tk.Combobox(root, values=["en", "fr", "de", "es", "it"]) languages.set("Select a Language") languages.pack() def translate(): input_text_value = input_text.get() translator = Translator() output_text.config(text=translator.translate(input_text_value, dest=languages.get()).text) translate_button = tk.Button(root, text="Translate", command=translate) translate_button.pack() root.mainloop() |
Learn More on Python GUI Development
- Build Text to Speech App with Python & PyQt5
- How to Build GUI Window in PyQt6
- How to Show Image in PyQt6
- How to Create Calendar with Python & PyQt6
- How to Create CheckBox in PyQt6
Please note that this is a basic example, and there are many ways to create a translator with Python. The specific implementation will depend on your use case. Additionally, you can use the googletrans
library for free but with a limitation of usage, you can get more information about the pricing from the official website.