吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1296|回复: 33
收起左侧

[Python 原创] 汉字转拼音支持首字母大写、分行分隔输出、读取复制剪切板

  [复制链接]
你怎么长不高啊 发表于 2024-3-29 18:23
本帖最后由 你怎么长不高啊 于 2024-3-29 18:28 编辑

这是一个对大多数人没什么用的小工具,只是分享一下,如果对你有用请留下评分谢谢!
成品如下图,成品链接:https://www.alipan.com/s/eR3ep37x49f 提取码: jq06
图片.png 图片.png
源码自取
[Python] 纯文本查看 复制代码
import tkinter as tk
from tkinter import ttk
from ttkthemes import ThemedTk
from pypinyin import pinyin, Style
from PIL import ImageTk, Image
import pyperclip

def show_tooltip(event):
    widget = event.widget
    widget.tooltip_window = tk.Toplevel(widget)
    widget.tooltip_window.wm_overrideredirect(True)
    widget.tooltip_window.wm_geometry("+%d+%d" % (event.x_root+20, event.y_root+10))
    tooltip_label = tk.Label(widget.tooltip_window, text=widget.tooltip_text, background="#ffffe0", relief="solid", borderwidth=1)
    tooltip_label.pack()

def hide_tooltip(event):
    event.widget.tooltip_window.destroy()

def add_tooltip(widget, tooltip_text):
    widget.tooltip_text = tooltip_text
    widget.bind("<Enter>", show_tooltip)
    widget.bind("<Leave>", hide_tooltip)

def hanzi_to_pinyin(hanzi, capitalize_first_letter=False, use_spacing=True):
    full_pinyin = (' ' if use_spacing else '').join([item[0].capitalize() if capitalize_first_letter else item[0] for item in pinyin(hanzi, style=Style.NORMAL)])
    return full_pinyin

def convert():
    hanzi = hanzi_entry.get()
    capitalized = capitalized_var.get()
    use_spacing = spacing_var.get()
    full_pinyin = hanzi_to_pinyin(hanzi, capitalized, use_spacing)
    full_pinyin_text.delete(1.0, tk.END)
    full_pinyin_text.insert(tk.END, full_pinyin)

def convert_from_clipboard():
    try:
        clipboard_content = root.clipboard_get()
    except tk.TclError:
        clipboard_content = ""
    hanzi_entry.delete(0, tk.END)
    hanzi_entry.insert(0, clipboard_content)
    convert()

def copy_to_clipboard():
    full_pinyin = full_pinyin_text.get("1.0", 'end-1c')
    pyperclip.copy(full_pinyin)

root = ThemedTk(theme='equilux')
root.title('汉字转拼音小程序')
window_width = 500
window_height = 400
root.geometry(f'{window_width}x{window_height}')

# setup window position in the center of the screen
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
position_top = (screen_height // 2) - (window_height // 2)
position_right = (screen_width // 2) - (window_width // 2)
root.geometry("+%d+%d" % (position_right, position_top))

main_frame = tk.Frame(root)
main_frame.pack(fill=tk.BOTH, expand=True)

hanzi_entry_label = tk.Label(main_frame, text='请输入汉字:')
hanzi_entry = tk.Entry(main_frame)
add_tooltip(hanzi_entry, '在这里输入汉字')

capitalized_check_label = tk.Label(main_frame, text='全拼首字母大写:')
capitalized_var = tk.BooleanVar()
capitalized_check = tk.Checkbutton(main_frame, var=capitalized_var)

spacing_check_label = tk.Label(main_frame, text='使用分隔符:')
spacing_var = tk.BooleanVar()
spacing_var.set(True)
spacing_check = tk.Checkbutton(main_frame, var=spacing_var)

convert_button = tk.Button(main_frame, text='转换', command=convert)
clipboard_button = tk.Button(main_frame, text='从剪切板转换', command=convert_from_clipboard)
copy_button = tk.Button(main_frame, text="复制结果到剪切板", command=copy_to_clipboard)

full_pinyin_label = tk.Label(main_frame, text='全拼拼音:')
full_pinyin_text = tk.Text(main_frame)

hanzi_entry_label.grid(row=0, column=0, padx=5, pady=5, sticky='w')
hanzi_entry.grid(row=0, column=1, padx=5, pady=5, sticky='ew')

capitalized_check_label.grid(row=1, column=0, padx=5, pady=5, sticky='w')
capitalized_check.grid(row=1, column=1, padx=5, pady=5)

spacing_check_label.grid(row=2, column=0, padx=5, pady=5, sticky='w')
spacing_check.grid(row=2, column=1, padx=5, pady=5)

convert_button.grid(row=3, column=0, padx=5, pady=5)
clipboard_button.grid(row=3, column=1, padx=5, pady=5)

full_pinyin_label.grid(row=4, column=0, padx=5, pady=5, sticky='w')
full_pinyin_text.grid(row=5, column=0, columnspan=2, padx=5, pady=5, sticky='nsew')

copy_button.grid(row=6, column=0, padx=5, pady=5)

main_frame.columnconfigure(1, weight=1)
main_frame.rowconfigure(5, weight=1)

root.mainloop()

免费评分

参与人数 9吾爱币 +13 热心值 +8 收起 理由
YYJ007 + 1 谢谢@Thanks!
爱飞的猫 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
luozi1653 + 1 + 1 热心回复!
观弈山人 + 1 谢谢@Thanks!
ysy2001 + 1 + 1 热心回复!
华桥 + 1 + 1 我很赞同!
jtjt68 + 1 我很赞同!
collinchen1218 + 1 + 1 厉害
sdx520521 + 1 + 1 我很赞同!

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

lccccccc 发表于 2024-3-29 20:06

我研究了一下,把25行:

    full_pinyin = (' ' if use_spacing else '').join([item[0].capitalize() if capitalize_first_letter else item[0] for item in pinyin(hanzi, style=Style.NORMAL)])

改成:

    full_pinyin = (' ' if use_spacing else '').join([item[0].capitalize() if capitalize_first_letter else item[0] for item in pinyin(hanzi, style=Style.TONE)])

即可


免费评分

参与人数 2吾爱币 +6 热心值 +2 收起 理由
你怎么长不高啊 + 3 + 1 用心讨论,共获提升!
collinchen1218 + 3 + 1 牛!

查看全部评分

apaye 发表于 2024-3-30 07:53
collinchen1218 发表于 2024-3-29 18:37
zaifen 发表于 2024-3-29 18:38
确实有一套,虽然没有用,先收藏起来,谢谢。
Swell5 发表于 2024-3-29 18:41
确实有一套,虽然没有用,先收藏起来
bigmud 发表于 2024-3-29 18:42
对小学1-2年级可能有点用,可惜没有声调
52soft 发表于 2024-3-29 18:59
能讲解一下转拼音的方法吗?
moruye 发表于 2024-3-29 20:19
确实有一套
collinchen1218 发表于 2024-3-29 20:30
lccccccc 发表于 2024-3-29 20:06
[md]
我研究了一下,把25行:
```python

其实最好是允许设置音调格式,允许设置不加标调(主楼上的),加正常标调(就是截图的那种),还有数字标调的,如:吾(wu2)
华桥 发表于 2024-3-29 20:49
如果能同时支持转英文,还有双语同时可以复制,对于做视频设计和图片设计来说比较方便的,以前我都是在网页上直接搞
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-29 14:04

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表