Building a Text-to-Speech Application using Python and Tkinter
In this blog post, we will explore how to build a simple text-to-speech application using Python and Tkinter. The application will allow users to enter text and have it converted to speech with the click of a button. We will be using the pyttsx3
library for text-to-speech conversion and the tkinter
library for creating the graphical user interface.
Prerequisites
Before we begin, make sure you have Python installed on your system. You can download and install Python from the official website at https://www.python.org/. Additionally, we’ll need to install the pyttsx3
library, which can be installed using pip
by running the following command:
pip install pyttsx3
Importing the Required Libraries
Let’s start by importing the necessary libraries for our text-to-speech application:
import pyttsx3
import tkinter as tk
import threading
The pyttsx3
library provides a simple interface for text-to-speech conversion. tkinter
is a standard Python library for creating graphical user interfaces. We'll use threading
to perform text-to-speech in a separate thread so that our application remains responsive while speaking.
Creating the Speak Function
Next, let’s define the speak_text
function, which will be called when the user clicks the "Speak" button. This function will read the text from the text entry field, initialize the text-to-speech engine, and speak the text:
def speak_text():
def speak():
text = text_entry.get("1.0", tk.END)
engine = pyttsx3.init()
engine.say(text)
engine.runAndWait()
# Re-enable the button after speech is finished
speak_button.config(state=tk.NORMAL)
# Disable the button while speaking
speak_button.config(state=tk.DISABLED)
# Create a new thread for speaking
speak_thread = threading.Thread(target=speak)
speak_thread.start()
In this function, we first retrieve the text entered by the user from the text entry field using the get
method of the text_entry
widget. We then initialize the text-to-speech engine using pyttsx3.init()
.
Next, we use the say
method of the engine to set the text to be spoken. We pass the retrieved text to this method. The runAndWait
method is then called to start the speech synthesis and wait until it finishes.
After the speech is finished, we re-enable the “Speak” button by setting its state to tk.NORMAL
.
To ensure that the application remains responsive while speaking, we disable the “Speak” button by setting its state to tk.DISABLED
. This prevents the user from triggering multiple speech requests before the previous one has finished.
Finally, we create a new thread using threading.Thread
and passing the speak
function as the target. We start the thread by calling its start
method.
Creating the Application Window
Now let’s create the main application window using Tkinter:
window = tk.Tk()
window.title("Text-to-Speech Application")
window.geometry("800x300")
Here, we create a new Tk
object set the window title to "Text-to-Speech Application," and define the window dimensions as 800 pixels wide and 300 pixels high.
Creating the Text Entry Field and Speak Button
Next, we’ll create the text entry field and the “Speak” button:
text_entry = tk.Text(window, height=13, width=35)
text_entry.pack()
speak_button = tk.Button(window, text="Speak", command=speak_text)
speak_button.pack()
We use the Text
widget to create a multi-line text entry field with a height of 13 lines and a width of 35 characters. We then use the pack
method to add it to the window.
The Button
widget is used to create the "Speak" button. We set its label to "Speak" and associate the speak_text
function as the command to be executed when the button is clicked.
Running the Application
Finally, we run the application by calling the mainloop
method of the window object:
window.mainloop()
The mainloop
method starts the Tkinter event loop, which waits for events such as button clicks, mouse movements, and key presses. It ensures that our application remains active and responsive to user interactions.
Source code: GitHub