I was wondering if i could output something that the windows narrator can use to narrate my tkiner application.
import tkinter as tk
# create root window
root = tk.Tk()
root.title("Test")
lbl = tk.Label(root, text = "A label")
lbl.grid()
root.mainloop()
Taking this as a example, lets say a mouse is hovering over the label, the narraor will read, "A label".
There are methods for doing optical text recognition through pip libraries, but I was wondering if I could do ocr without downloading modules, and through the use of windows narrator.
You can use the pywin32
package, with SAPI SpVoice.
I have bound the <Enter>
attribute to speak whenever you hover over the label. You could of course use different triggers if you so desire.
import tkinter as tk
import win32com.client
# create root window
root = tk.Tk()
root.title("Test")
lbl = tk.Label(root, text="A label")
lbl.grid()
speaker = win32com.client.Dispatch("SAPI.SpVoice")
lbl.bind("<Enter>", lambda _: speaker.Speak(lbl.cget("text")))
root.mainloop()
If you want to generalise this solution such that all your labels are narrated, you could subclass the tk.Label
, like so
import tkinter as tk
import win32com.client
speaker = win32com.client.Dispatch("SAPI.SpVoice")
class NarratedLabel(tk.Label):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.bind("<Enter>", lambda _: speaker.Speak(self.cget("text")))
# create root window
root = tk.Tk()
root.title("Test")
lbl = NarratedLabel(root, text="A label")
lbl.grid()
lbl2 = NarratedLabel(root, text="Another label")
lbl2.grid()
root.mainloop()