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