Building a Text-to-Speech App with Python, Tkinter, and gTTS

Title: Building a Text-to-Speech App with Python, Tkinter, and gTTS

Introduction:
Welcome to another exciting Python programming tutorial! In this post, we will explore how to create a text-to-speech app using Python, Tkinter, and the gTTS (Google Text-to-Speech) library. With just a few lines of code, you’ll be able to convert text to audio and even listen to it without saving the files. Let’s dive in!

Setting Up the Environment:
Before we begin, let’s make sure we have all the necessary components installed. We’ll need Python, Tkinter, pygame, and the gTTS library. We’ll guide you through the installation process and ensure you’re ready to proceed.

Creating the App:

  1. GUI Setup:
    We’ll start by setting up the graphical user interface (GUI) using Tkinter. We’ll create a window and add some labels and entry fields to allow the user to input text and choose a language for the audio.
  2. Text-to-Speech Conversion and Playback:
    Using the gTTS library, we’ll convert the entered text into an audio file. Additionally, we’ll utilize the pygame library to play the audio directly without saving it as a file. We’ll create functions to handle these processes.
  3. Listening to Audio:
    To enable users to listen to the audio without saving the file, we’ll implement a “Listen” button that triggers the text-to-speech conversion and audio playback functions. This feature enhances the user experience and eliminates the need for file management.
  4. Saving the Audio as an MP3 File:
    For those who prefer to save the audio as an MP3 file, we’ll implement a “Create MP3” button. When clicked, this button will convert the text to audio, save it with a user-defined filename, and open the saved file for playback.

Conclusion:
Congratulations! You’ve successfully built a text-to-speech app using Python, Tkinter, and gTTS. This app allows you to convert text to audio and listen to it directly or save it as an MP3 file for future use. You can further customize and enhance the app by adding more features or incorporating it into larger projects.

We hope this tutorial has provided you with valuable insights into Python application development and the power of text-to-speech technology. Feel free to explore the code, modify it to suit your needs, and continue building upon this foundation.

If you have any questions or suggestions, please leave a comment below. Happy coding!

Note: This code was originally written by Giovanni Gatto, also known as Educational Channel on YouTube and PythonProgrammi on Twitter. You can find the original code and additional resources on Giovanni’s website: http://pythonprogramming.altervista.org .

This is the code

"""Use gtts with Python.

Create mp3 from text with python, tkinter and gtts
2018
@ Giovanni Gatto
"""

# A PROGRAM FROM GIOVANNI GATTO AKA EDUCATIONAL CHANNEL ON YOUTUBE
# AKA PYTHONPROGRAMMI ON TWITTER
# published on http://pythonprogramming.altervista.org

from gtts import gTTS
import os
import tkinter as tk
from io import BytesIO 
import pygame
import time


pygame.init()
pygame.mixer.init()
root = tk.Tk()
root.geometry("400x400+500+100")


def wait():
    while pygame.mixer.get_busy():
        time.sleep(.1)

def speak():
    global text, lan
    mp3_fo = BytesIO()
    tts = gTTS(text.get(0.0, tk.END), lang=lan.get())
    tts.write_to_fp(mp3_fo)
    mp3_fo.seek(0)
    sound = pygame.mixer.Sound(mp3_fo)
    sound.play()
    wait()



# THE ACTION LINKED TO THE EVENT LISTENER
def create_mp3(lang="en"):

    print(lan.get())
    if lan.get() != None:
        lang = str(lan.get())
    s = gTTS(text.get(0.0, tk.END), lang=lang)
    s.save(f"{str(mp3.get())}.mp3")
    filename = mp3.get() + ".mp3"
    os.system("start " + filename)

# THE FIRST LABEL TELLS YOU WHAT TO DO TO MAKE IT WORK
title = tk.Label(root, text="TEXT TO SPEECH WITH GOOGLE API")
title.pack()
title['bg'] = 'yellow'
# WHERE YOU WRITE THE TEXT

# =========================================
# ENTRY FOR THE LANGUAGE
    # LABEL
l2 = tk.Label(root, text="Choose language (default is english=en)")
l2.pack()
    # THE ENTRY WIDGET
lan = tk.StringVar() # goes into textvariable
lan.set("en") # default value
language = tk.Entry(root, textvariable=lan, width=2)
language.pack()
language.focus()

# ===========================================
# THE NAME OF THE FILE
l3 = tk.Label(root, text="Insert the mp3 name, without the filetype .mp3")
l3.pack()
mp3 = tk.StringVar()
mp3title = tk.Entry(root, textvariable=mp3, width=50)
mp3title.pack()
mp3.set("myfile")


speak_but = tk.Button(root,
    text=f"Listen Audio without saving the mp3 file",
    bg="lightgreen",
    command=speak)
speak_but.pack()

# THE EVENT LISTENER
# root.bind("<Return>", lambda x: create_mp3())

t = tk.StringVar()
text = tk.Text(root, bg="gold", height=5)
text.pack()
text.insert("0.0", "Hello")

mp3_but = tk.Button(root,
    text=f"Create the mp3 file",
    bg="lightgreen",
    command=create_mp3)
mp3_but.pack()

root.mainloop()

This is how the gui looks like

If you want to support me, you can buy for 5 € the exe file of this tool on itch.io


Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.