Crafting a text-to-speech converter with Python is like teaching your computer to speak. Imagine taking a piece of text, maybe your favorite book excerpt or a note you’ve written, and then, with a little bit of coding magic, hearing it read back to you in clear, natural tones.
In today’s tutorial, we are going to write code that creates a basic text-to-speech converter with a graphical user interface (GUI) in which users can enter text into a text field, click a button, and the program will convert that text into speech using the pyttsx3 library.
Let’s get started!
Table of Contents
- Necessary Libraries
- Imports
- Initializing pyttsx3
- text-to-speech Function
- Creating the Main Window
- Designing the Input Section
- Adding the Conversion Button
- Running the Application
- Example
- Full Code
Necessary Libraries
First, make sure to install these libraries from the terminal or your command prompt for the code to function properly:
$ pip install tk
$ pip install pyttsx3
Imports
We start by importing everything (*
) from the tkinter library, which allows us to create a graphical user interface (GUI). Additionally, we import the pyttsx3
library which is used for text-to-speech conversion.
from tkinter import *
import pyttsx3
Initializing pyttsx3
Next, we create this code to initialize the text-to-speech converter
from the pyttsx3
library, which you’ll use to convert text into speech.
converter = pyttsx3.init()
text-to-speech Function
Following that, we create this function that is responsible for converting text to speech. It starts by retrieving the text entered by the user in the GUI’s text entry field. Then, it sets a conditional statement that checks if the text variable is not empty, meaning the user has entered some text. If the text is not empty, it uses the converter
to convert the text to speech and finally ensures the text-to-speech conversion is executed and spoken out immediately.
def generate_vocals():
text = text_entry.get()
if text:
converter.say(text)
converter.runAndWait()
Creating the Main Window
root = Tk()
root.title("text to speech converter - The Pycodes")
root.geometry("400x200")
This part of the code creates a tkinter window (GUI), stores it in the root variable then sets the title of the GUI window to “text to speech converter – The Pycodes” After that, it defines the size of the GUI window, which is 400
pixels wide and 200
pixels tall.
Designing the Input Section
Now, we create a frame, which serves as a container for holding other GUI elements. Then, we pack this frame within the window, adding some padding (space) around it to ensure it fills both horizontally and vertically (BOTH
). After that, we introduce a label with the text “Enter your Text” in Arial font, size 14, and place it within the frame.
Next, we craft a text entry field using the Entry widget, specifying a font style of Helvetica, size 14. Finally, we pack the text entry field with some padding, making it expand both horizontally and vertically within the frame.
frame = Frame(root)
frame.pack(padx=20,pady=30,fill=BOTH,expand=True)
label = Label(frame,text="Enter your Text :",font=("Arial",14))
label.pack()
text_entry = Entry(frame,font=("helvetica",14))
text_entry.pack(padx=10,pady=10,fill=BOTH,expand=True)
Adding the Conversion Button
Here, we created a button labeled “convert to speech” with the command to execute the generate_vocals
function when clicked. It also sets the background color to orange and the text color to green, The button is added to the frame using the pack method.
convert_button = Button(frame,text="convert to speech",command=generate_vocals,bg="orange",fg="green",font=("helvetica",14))
convert_button.pack()
Running the Application
Finally, we run the main event loop of the GUI, allowing it to respond to user interactions and remain open until the user closes the window.
root.mainloop()
Example
Full Code
from tkinter import *
import pyttsx3
converter = pyttsx3.init()
def generate_vocals():
text = text_entry.get()
if text:
converter.say(text)
converter.runAndWait()
root = Tk()
root.title("text to speech converter - The Pycodes")
root.geometry("400x200")
frame = Frame(root)
frame.pack(padx=20,pady=30,fill=BOTH,expand=True)
label = Label(frame,text="Enter your Text :",font=("Arial",14))
label.pack()
text_entry = Entry(frame,font=("helvetica",14))
text_entry.pack(padx=10,pady=10,fill=BOTH,expand=True)
convert_button = Button(frame,text="convert to speech",command=generate_vocals,bg="orange",fg="green",font=("helvetica",14))
convert_button.pack()
root.mainloop()
Happy Coding!