Text to speech converter GUI with Python

Lakshmisowjanya Garapati
Explore Data Science

--

Convert Text to Audio

Hello Techies, Welcome. In this article, we will be discussing about converting a text input to audio output using python and tkinter. So, let’s get started.

Prerequisites:

Let’s understand what are these for..

Tkinter: Tkinter is the python module which supports the creation of GUI applications. Confused? Let me clarify.

What is a GUI Application?:

GUI stands for Graphical User Interface. Python has the amazing module tkinter for creating the user interface application. A user interface is basically the UI or frontend of the application with which the user interacts. It contains the Buttons, fields, Checkboxes etc. We will be creating one such very simple application in this project.

Run the following command to install tkinter.

You can find more details about tkinter module here

gtts: gtts stands for google translate text to speech . This module is used for converting the text to speech. In this project we will import the gTTS library to use the google text to speech API. Run the following command to install gtts.

os: os module is used to perform the operations on the files of your local computer. Following command installs os module.

Yayy!! Ready to go. Now, let’s start coding.

The code is available at my Github repo

Step 1: Importing modules

The above code will import all the required modules.

Step 2: Creating Frame

Frames are the main windows which the user sees when the application is opened.

The tk.Tk() takes a className to create the main windows to the application and stores it in the master variable. Then we will use the geometry function to define the dimensions of the main window. Next Frame() uses the master variable to make a Frame. The pack() method arranges the widgets in blocks and places them in the main frame mainFrame

Step 3: Creating a text box for user input

We will use tk.StringVar() to get the contents of the text entered in textbox as a string and store it in myText_str variable. The Label() function takes the arguments like frame object mainFrame and text to give a label name to textbox and arranges it in grid using .grid(). Next we need an input text box which will be created using the Entry() method that takes the previously created string myText_str in textvariable argument, bd for dimensions and bg for background color. Then the entry box will be arranged in a grid with myText.grid()

Step 4: Text to Speech conversion

In this step, we will create a function text_to_speech which converts the text into speech and then saves it in the system. First, we specify a language in which the audio needs to be pronounced. Then we need to get the entered text from the input box, which can be done by myText_str.get(). In the next line, we use if condition to check whether the user entered any input or not. This is because an empty input cannot produce any audio output. If the user entered an empty string then we will show an error messagebox messagebox.showerror("Error","you need to enter some text to get audio output"). Otherwise, we will say the user about the entered text. Now, the magic happens. The gTTS() method takes the TextEntered variable which contains the string input text from the user, and language in the lang argument. That's it. Now we will save the converted object using obj.save('filename.mp3'). to save it in the system use os.system('filename.mp3'). The audio file will be saved in your project location.

Hmm, missed something??

On what action does the text will be converted to audio? Okay, now scroll down.

So, we will be creating a button. when the user clicks on it after entering the input he will get the audio output. We will do the same thing as for text box that is creating the frame and packing it. Then we will create the actual button convert_button which takes the following arguments.

text- text to be appeared on button

fg- color of the text

activebackground- button background color

activeforeground-button foreground color

width-button width

bg- button color

borderwidth-button border

command-action to perform (here we will pass the text_to_speech function)

Don’t forget to grid and pack the button.

So the final step → master.mainloop() it runs the event loop. This is the important line to be included in the tkinter app.

Step 5: Now, it’s time to run the code.

python textToSpeech.py

which opens the following window.

messagebox after entering the text.

Error message on empty text.

You can find the complete code on my Github.

Found this post useful? Kindly tap the 👏 button below! Suggestions/ideas are welcome in the comment box :)

Thanks for reading❤️.

Click here to know more about me.

Have a Good Day :)

Originally published at https://lakshmisowjanya.hashnode.dev.

--

--