SPEAK
We are going to make a GUI with tkinter to make the pc speak on Windows with the SAPI.SpVoice and with the module win32com. We’ve seen this code before, but now we add a GUI and later we will make something a little more complex.
- install win32com module with:
- pip install pypiwin32
First let’s import the module
from win32com.client import Dispatch
Then we make the computer able to speak connecting python to the API of the syntethic voice
s = Dispatch("SAPI.SpVoice")
Now we need a GUI with tkinter; we import the module and create a function to speak
import tkinter as tk def talk(x): s.Speak(x.get("1.0",tk.END))
And then we add the widgets (a label, a button and a text box):
root = tk.Tk() root.title("My Syntethic voice") root.geometry("300x200+200+300") label = tk.Label(root, text="ENTER TEXT BELOW") label.pack() text = tk.Text(root) button = tk.Button(root, text="Click to speak", command=lambda : talk(text)) button.pack() text.pack() text.focus() root.mainloop()
The entire code
from win32com.client import Dispatch import tkinter as tk #speak = Dispatch("SAPI.SpVoice") speak = Dispatch("SAPI.SpVoice") def talk(x): speak.Speak(x.get('1.0',tk.END)) root = tk.Tk() root.title("Speak with Python") root.geometry("300x300") label = tk.Label(root, text="ENTER TEXT BELOW").pack() entry = tk.Text(root) entry.insert("1.0","Ciao") button = tk.Button(root, text="speak", command=lambda : talk(entry)) button.pack() entry.pack() entry.focus() root.mainloop()
The video about the GUI in tkinter to make the pc Speak