In this lesson i want to show you How to Build Simple Translator with Python , Building simple translator with Python involves several steps:
- Import necessary libraries: you will need to import the googletrans library for translation, and tkinter library for creating the GUI.
1 2 |
from googletrans import Translator import tkinter as tk |
- Create 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 and Label to receive the text to translate and display the translated text. you can also create Combobox 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 translate function: you can create function that will take the input text, target language and use Translator class from the googletrans 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 button that when clicked, it calls 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 basic example and there are many ways to create translator with Python. specific implementation will depend on your use case. also 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.