How to make a Tkinter GUI for text to speech

Example to use tkinter for a GUI to make the pc talk with text to speech from gtts module.

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


pygame.init()
pygame.mixer.init()
def speak(text, language='en'):
    mp3_fo = BytesIO()
    tts = gTTS(text, lang=language)
    tts.write_to_fp(mp3_fo)
    x = pygame.mixer.music.load(mp3_fo, 'mp3')
    pygame.mixer.music.play()


root = tk.Tk()

tx = tk.Text(root)
tx.pack()
bt = tk.Button(root, text="PLAY",
    command=lambda: speak(tx.get('0.0', tk.END)))
bt.pack()
root.mainloop()
on repl.it

https://replit.com/@EducationalChan/speak


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.