Tutorial: Program a text translator with graphical interface in Python (Source code)

In this new tutorial I will teach you to program a text translator in the Python programming language, which will have a graphical interface to make this more striking. The translator will allow text input, You can select the language in which we want to translate it and while it is being translated, a loading window will be displayed. Let us begin.

The first thing to do is install the necessary libraries if we do not have them, we are mainly going to use the library googletrans which will allow us to carry out text translations.

What's more, we will also use PySimpleGUI for the creation of graphical interfaces, this library is not mandatory to install, since in the source code I will include the library to import directly.

We execute the installation of googletrans:

pip install googletrans

Once the library is installed, we are going to import it along with the other necessary libraries that are already included in Python:

Now, it will be necessary to create a class called translator, which will have the methods to translate the text that we enter, but first we must design our graphical interface with the help of PySimpleGUI, We will define the graphical aspects and properties of the class in the __init__ method:

The first lines of code will help us to obtain the list of languages ​​in which we can translate our text, we will also define a couple of frames, which will serve to contain a MultilineTex type object that will serve to enter the text to be translated and to display it once translated.

[jetpack_subscription_form show_only_email_and_button=”true” custom_background_button_color=”undefined” custom_text_button_color=”undefined” submit_button_text=”Subscribe to receive all our tutorials” submit_button_classes=”undefined” show_subscribers_total=”false” ]

We will add a couple of ComboBox, these will contain a list of available languages: one will be for the source language of the text and the second will be for the target language of the translation.

We will also create an about frame, which will contain contact information. By last, We will declare two more properties: translation_state and translation_text.

The variable ‘state_translation’ will help us to identify the current state of the translation operation, a True indicates that the translation is taking place, and a False the opposite. ‘Text_translation’ will contain the text resulting from the translation.


Now comes the interesting part, we will create some methods that will help us in the translation process, the first one will be 'translate', which will allow you to translate the text entered:

This method will receive 3 input parameters: the text to translate, the source language and the target language. We will add a try catch to avoid connection problems, in both cases the variable text_translation will be updated with the resulting text.

The following method called ‘detect_language’ will help us to identify the language of a text in case of not knowing which one it belongs to, in addition to being able to translate it to the language of our preferences:

This method will only receive 2 parameters: The text to be translated and the target language. The method will automatically identify the source language of the text whenever possible.

[jetpack_subscription_form show_only_email_and_button=”true” custom_background_button_color=”undefined” custom_text_button_color=”undefined” submit_button_text=”Subscribe to receive all our tutorials” submit_button_classes=”undefined” show_subscribers_total=”false” ]

The third method will allow us to obtain the LANGCODES of a selected language, This is very important since they are the values ​​that the translator uses to carry out the translation from one language to another..

This method is somewhat simple , but of utmost importance.

Finally, we will add one last method, which will function as a waiting window that will appear while we are translating a text.

This method is a minimalist design similar to the main interface, with the difference that it will only show an animated gif and a button to cancel the operation. The variable blue_dots contains a loading gif in base64, I have not been able to place it here due to its great length, but it is available in the source files.

The most important method is the one that will allow us to start the execution of our translator, which is the following:

The first lines of code will allow us to "pack" the graphical interface that we define in our init, in addition to being able to sort them in two main tabs. The execution of the interface will be in a while loop.

As long as the while loop is running, we will be reading the events of the graphical interface, one of them will be the one with the translate button, in case it is executed we will launch to call the translation methods, We will also use threads which will help us to show the loading screen while in the background we carry out the translation. The loading screen will only disappear if the translation was successful or an error occurred. If successful we will show the translated text.

The latest events will be used to launch the browser if you click on any of the buttons on the "about" tab..

This ends the explanation of your programming. Something very important is that we are not using the official Google API to translate, but a third-party library that performs this process for us. There is a limitation of 46 KB of size of the text to translate, In addition, if the service is abused, we could suffer an IP block of the service.

But it's not something to worry about, for practice and learning purposes it is more than enough, as always here we will leave you the link so you can download the source code and practice with it. Any questions or suggestions leave it in the comments.

Download source code

4 thoughts on “Tutorial: Program a text translator with graphical interface in Python (Source code)”

  1. Very good, but it would be good to explain the practical way of executing the program since I get errors when executing and it cannot compile the graphical interface, It would be good, thank you very much

Leave a Reply

Your email address will not be published. Required fields are marked *

three × three =