我想知道我是否可以输出一些东西,让Windows叙述者可以用来讲述我的更精简的应用程序.

import tkinter as tk
 
# create root window
root = tk.Tk()

root.title("Test")

 
lbl = tk.Label(root, text = "A label")
lbl.grid()
 
root.mainloop()

以此为例,假设一只鼠标悬停在标签上,说话人会读到"A Label".

有一些方法可以通过PIP库进行光学文本识别,但我想知道是否可以在不下载模块的情况下通过使用Windows讲述人来进行OCR.

推荐答案

您可以使用带有SAPI SpVoice的pywin32包.

我已经将<Enter>属性绑定为每当您将鼠标悬停在标签上时发言.当然,如果您愿意,您可以使用不同的触发器.

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()

如果您想对此解决方案进行泛化,以便对所有标签进行说明,则可以将tk.Label作为子类,如下所示

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()

Python相关问答推荐

pyramid 内部数组中的连续序列-两极

具有2D功能的Python十六进制图

给定数据点,制定它们的关系

使用FASTCGI在IIS上运行Django频道

Pandas实际上如何对基于自定义的索引(integer和非integer)执行索引

log 1 p numpy的意外行为

Python键入协议默认值

将tdqm与cx.Oracle查询集成

如何在表中添加重复的列?

Python脚本使用蓝牙运行在Windows 11与raspberry pi4

如何在FastAPI中为我上传的json文件提供索引ID?

为什么\b在这个正则表达式中不解释为反斜杠

dask无groupby(ddf. agg([min,max])?''''

在Python中计算连续天数

如何排除prefecture_related中查询集为空的实例?

重置PD帧中的值

Python—压缩叶 map html作为邮箱附件并通过sendgrid发送

导入错误:无法导入名称';操作';

polars:有效的方法来应用函数过滤列的字符串

在Django中重命名我的表后,旧表中的项目不会被移动或删除