We will learn how to create a floating application that converts Text-to-speech and reads the text out loudly.
So many times we have got a long list of pages that we want to read or important mail content that we want to read but reading it is very time-consuming as the text is many times very long. So instead of reading the whole document, you can just highlight the text and our application will read it out for you. This application becomes very handy if you have a long paragraph to read and you have very little time.
The application will run in the background and you can listen and continue other work simultaneously.
In this article, we will learn how to create a GUI application that will take any text as an input and will return the text into a Speech format.
We will first import all the required libraries.
from typing import runtime_checkable import pyttsx3 import random from tkinter import * from tkinter import ttk import tkinter as tk window = tk.Tk()
We'll then declare a class 'texttospeech' and will initialise the window and will set the parameters for the window such as size, origins and title.
class texttospeech: def __init__(self,window): self.window = window self.window.title("Text to Speech Application Widget ") self.window.geometry("500x200+0+0") title = Label(self.window,text="Text to Speech Application Widget",bd=0,font=("times new roman",15,"bold"),bg="white",fg="magenta") title.pack(side=TOP,fill=X)
Then we'll initialise 'mystring' as a string variable in the window.
mystring = tk.StringVar(window)
Then we define one entry for the text input, one button i.e. 'Speak' for the Speech conversion to be done and another button named 'Clear' to clear the text from the Entry field.
text1 = Entry(self.window,textvariable=mystring,width=100,fg="blue",bd=3,selectbackground='violet') text1.place(x=0,y=20,height=50,width=500) run_button = Button(self.window,text=" Speak ", command=Speech) run_button.place(x=20,y=100,height=50,width=70) clr_button = Button(self.window,text=" clear ", command=clear) clr_button.place(x=250,y=100,height=50,width=70)
Now let's define the Speech conversion function 'Speech()'
def Speech(): mes = mystring.get() # Get string which is in the Entry field of GUI. engine = pyttsx3.init('sapi5') # initialise the engine voices = engine.getProperty('voices') # Get the prerecorded voices engine.say('{}'.format(mes)) # make the engine speak the message engine.runAndWait()
Now we write a function 'clear()' that will clear the Entry field in the GUI.
def clear(): mystring.set("")
Finally, we enable the Tkinter GUI window.
ob = texttospeech(window) window.mainloop()
from typing import runtime_checkable import pyttsx3 import random from tkinter import * from tkinter import ttk import tkinter as tk window = tk.Tk() class texttospeech: def __init__(self,window): self.window = window self.window.title("Text to Speech Application Widget ") self.window.geometry("500x200+0+0") title = Label(self.window,text="Text to Speech Application Widget",bd=0,font=("times new roman",15,"bold"),bg="white",fg="magenta") title.pack(side=TOP,fill=X) mystring = tk.StringVar(window) def clear(): mystring.set("") def Speech(): mes = mystring.get() # Get string which is in the Entry field of GUI. engine = pyttsx3.init('sapi5') # initialise the engine voices = engine.getProperty('voices') # Get the prerecorded voices engine.say('{}'.format(mes)) # make the engine speak the message engine.runAndWait() text1 = Entry(self.window,textvariable=mystring,width=100,fg="blue",bd=3,selectbackground='violet') text1.place(x=0,y=20,height=50,width=500) run_button = Button(self.window,text=" Speak ", command=Speech) run_button.place(x=20,y=100,height=50,width=70) clr_button = Button(self.window,text=" clear ", command=clear) clr_button.place(x=250,y=100,height=50,width=70) ob = texttospeech(window) window.mainloop()
Submitted by Anay Ravindra Karanje (Anay250)
Download packets of source code on Coders Packet
Comments