生活中,会遇到很多如地名、人名等生僻字。本文通过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()
1288

被折叠的 条评论
为什么被折叠?



