python自动化脚本-显示生僻字读音

生活中,会遇到很多如地名、人名等生僻字。本文通过python生成了一个汉字转拼音的脚本,只需要通过鼠标选中生僻字,即可实现生僻字读音快速显示。

比如在呼叫系统中,呼叫人员经常遇到不认识的地名,人名,一般处理的方式就是复制出来,去问百度,高峰期的时候可能时间上来不及,这个时候可能会读错地名或人名,导致客诉,所以亟需开发一个快速查看生僻字读音的工具。具体如下:

地名生僻字:

 运行效果:

设计界面:

界面代码:

root = tk.Tk()
root.title(r"自动显示拼音")

root.attributes("-topmost", True)

root.geometry("250x100")  # 初始窗口大小

label = tk.Label(root, text="选中汉字或词语后自动显示拼音")
label.grid(row=0, column=0, sticky="nsew")  # sticky="nsew" 使标签填充整个单元格

# 为标签所在的行和列设置权重
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)

root.protocol("WM_DELETE_WINDOW", on_closing)

timer_pinyin = RepeatingTimer(2, showpinyin_task) # 10秒后执行my_task
timer_pinyin.start()

root.mainloop()

汉字转拼音代码:

text_pinyin = pinyin(text, heteronym=True)
label.config(text=text_pinyin)

完整代码:

from pypinyin import pinyin, Style
import pyperclip
import tkinter as tk
import time
from threading import Timer
import pyautogui

class RepeatingTimer(Timer):
    def run(self):
        while not self.finished.is_set():
            self.function(*self.args, **self.kwargs)
            self.finished.wait(self.interval)

def showpinyin_task():
    global label
    # 模拟Ctrl+C进行复制
    pyautogui.hotkey('ctrl', 'c')
    time.sleep(1)
    # 获取剪贴板内容
    text = pyperclip.paste()
    if text.strip():
        text_pinyin = pinyin(text, heteronym=True)
        label.config(text=text_pinyin)
    else:
        label.config(text="")

def on_closing():
    timer_pinyin.cancel()
    root.destroy()

root = tk.Tk()
root.title(r"自动显示拼音")

root.attributes("-topmost", True)

root.geometry("250x100")  # 初始窗口大小

label = tk.Label(root, text="选中汉字或词语后自动显示拼音")
label.grid(row=0, column=0, sticky="nsew")  # sticky="nsew" 使标签填充整个单元格

# 为标签所在的行和列设置权重
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)

root.protocol("WM_DELETE_WINDOW", on_closing)

timer_pinyin = RepeatingTimer(2, showpinyin_task) # 10秒后执行my_task
timer_pinyin.start()

root.mainloop()
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值