Type Reader App in Python: Pc reads the letters you type (tkinter)

In this little script I had this idea to let the computer say the letters you type. It is very rough and if you type fast the previous audio will stop in favour of the last key you press. There are also the sound for space (spazio in italian) and BackSpace (that I translated in indietro, back). You can improve this code and make something interesting maybe, or just take a look at it to get some ideas.

Look at this video that shows you the program. The audio will not be so loud, because I am recording from a mic, not the audio of the pc directly. When you will run the program it will be loud.

Create the mp3 for each letter

To create the letters in italian i did this:

# create the mp3 letters audio files

from gtts import gTTS

def create_mp3_files():
	letters = "abcdefghijklmnopqrstuvwxyz"
	for x in trange(len(letters)):
		t = gTTS(letters[x], "it")
		t.save(f"{letters[x]}.mp3")

create_mp3_files()

pip install gtts, if you haven’t that module. gtts is a module that uses the google API for the text-to-speech synthesis. This script creates all the audio files for the letters. This will go in the same folder of the script to make the computer talk.

To create other audio for keys like space of Backspace I used this function

def create_mp3(x):
	t = gTTS(x, "it")
	t.save(f"{x}.mp3")

create_mp3("spazio")
create_mp3("Indietro")

This is the code that makes the GUI to type into a text box while you hear the sound of the letters typed:

def start():
	mixer.init()

	def audiokey(event):
		print(event.char)
		try:
			if event.char == " ":
				event.char = "spazio2"
			if event.char == b"\x08":
				event.char == "indietro"
			mixer.music.load(event.char + ".mp3")
			mixer.music.play()
		except:
			pass
	def indietro(event):
		print(event.char)
		try:
			mixer.music.load("indietro.mp3")
			mixer.music.play()
		except:
			pass

	root = tk.Tk()
	text = tk.Text(root)
	text.pack()
	text.focus()
	text.bind("<Key>", audiokey)
	text.bind("<BackSpace>", indietro)
	root.mainloop()

start()
This is how the GUI looks like

I have all the code in one script, like this

from gtts import gTTS
import tkinter as tk
from pygame import mixer  # Load the popular external library
from tqdm import trange


def create_mp3(x):
	t = gTTS(x, "it")
	t.save(f"{x}.mp3")

def create_mp3_files():
	letters = "abcdefghijklmnopqrstuvwxyz"
	for x in trange(len(letters)):
		t = gTTS(letters[x], "it")
		t.save(f"{letters[x]}.mp3")

# Uncomment the following line to create the files the first time
#create_mp3_files()
#create_mp3("indietro")
def start():
	mixer.init()

	def audiokey(event):
		print(event.char)
		try:
			if event.char == " ":
				event.char = "spazio2"
			if event.char == b"\x08":
				event.char == "indietro"
			mixer.music.load(event.char + ".mp3")
			mixer.music.play()
		except:
			pass
	def indietro(event):
		print(event.char)
		try:
			mixer.music.load("indietro.mp3")
			mixer.music.play()
		except:
			pass

	root = tk.Tk()
	text = tk.Text(root)
	text.pack()
	text.focus()
	text.bind("<Key>", audiokey)
	text.bind("<BackSpace>", indietro)
	root.mainloop()

start()

It is very rough, but maybe we could make it better in the next days.

Using Sound class to play souds instead

The following code uses mixer.Sound, instead of mixer.music.load. Now the last sound will not stop the previous sound, so that there could be some overlapping. Experiment the two versions to see which one is better for your app.

from gtts import gTTS
import tkinter as tk
from pygame import mixer  # Load the popular external library
from tqdm import trange


def create_mp3(x):
	t = gTTS(x, "it")
	t.save(f"{x}.mp3")

def create_mp3_files():
	letters = "abcdefghijklmnopqrstuvwxyz"
	for x in trange(len(letters)):
		t = gTTS(letters[x], "it")
		t.save(f"{letters[x]}.mp3")

# Uncomment the following line to create the files the first time
#create_mp3_files()
#create_mp3("indietro")
def start():
	mixer.init()

	def audiokey(event):
		print(event.char)
		try:
			if event.char == " ":
				event.char = "spazio2"
			if event.char == b"\x08":
				event.char == "indietro"
			sound = mixer.Sound(event.char + ".mp3")
			sound.play()
			
		except:
			pass
	def indietro(event):
		print(event.char)
		try:
			sound = mixer.Sound("indietro.mp3")
			sound.play()
		except:
			pass

	root = tk.Tk()
	text = tk.Text(root)
	text.pack()
	text.focus()
	text.bind("<Key>", audiokey)
	text.bind("<BackSpace>", indietro)

	root.mainloop()

start()

Video comparison of the two scripts

The pygame mixer documentation

Go to this page to learn about the mixer from pygame module used in this page:

link to the mixer to play sounds

 

Tkinter test for students

Tkinter articles

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.