95 lines
2.6 KiB
Python
95 lines
2.6 KiB
Python
import tkinter as tk
|
|
from tkinter import ttk, messagebox
|
|
|
|
def copy_to_clipboard():
|
|
root.clipboard_clear()
|
|
root.clipboard_append(output_var.get())
|
|
messagebox.showinfo("成功", "已复制到剪贴板")
|
|
|
|
def convert_unicode(event=None):
|
|
s = entry.get().strip()
|
|
if not s:
|
|
messagebox.showwarning("提示", "请输入内容")
|
|
return
|
|
|
|
try:
|
|
unicode_str = ''.join(f"{ord(c):04X}" for c in s)
|
|
output_var.set(unicode_str)
|
|
# copy_to_clipboard()
|
|
except Exception as e:
|
|
messagebox.showerror("错误", f"转换失败: {str(e)}")
|
|
|
|
|
|
# 创建主窗口
|
|
root = tk.Tk()
|
|
root.title("Unicode转换器")
|
|
root.geometry("300x300") # 正方形窗口
|
|
# root.resizable(False, False)
|
|
root.configure(bg="#f5f6fa")
|
|
|
|
# 修复的变量定义
|
|
output_var = tk.StringVar()
|
|
|
|
# 现代界面样式配置
|
|
style = ttk.Style()
|
|
style.theme_use('clam')
|
|
|
|
# 自定义样式
|
|
style.configure("TFrame", background="#f5f6fa")
|
|
style.configure("TLabel",
|
|
font=("微软雅黑", 11),
|
|
background="#f5f6fa",
|
|
foreground="#2d3436")
|
|
style.configure("TEntry",
|
|
font=("微软雅黑", 11),
|
|
padding=5)
|
|
style.configure("TButton",
|
|
font=("微软雅黑", 10, "bold"),
|
|
padding=8,
|
|
foreground="#ffffff")
|
|
style.map("TButton",
|
|
background=[("active", "#0984e3"), ("!active", "#74b9ff")],
|
|
foreground=[("active", "#ffffff"), ("!active", "#ffffff")])
|
|
|
|
# 主容器
|
|
main_frame = ttk.Frame(root)
|
|
main_frame.pack(expand=True, padx=30, pady=30)
|
|
|
|
# 输入区域
|
|
input_frame = ttk.Frame(main_frame)
|
|
input_frame.pack(pady=15, fill="x")
|
|
|
|
ttk.Label(input_frame, text="输入文本:").pack(side="left", padx=(0, 10))
|
|
entry = ttk.Entry(input_frame, width=20)
|
|
entry.pack(side="left", expand=True)
|
|
entry.bind("<Return>", convert_unicode) # 绑定回车键
|
|
|
|
# 输出区域
|
|
output_frame = ttk.Frame(main_frame)
|
|
output_frame.pack(pady=20, fill="x")
|
|
|
|
ttk.Label(output_frame, text="转换结果:").pack(side="left", padx=(0, 10))
|
|
output_entry = ttk.Entry(
|
|
output_frame,
|
|
textvariable=output_var,
|
|
width=24,
|
|
state="readonly",
|
|
font=("Consolas", 12, "bold"),
|
|
foreground="#2d3436"
|
|
)
|
|
output_entry.pack(side="left", expand=True)
|
|
|
|
# 按钮区域
|
|
btn_frame = ttk.Frame(main_frame)
|
|
btn_frame.pack(pady=30)
|
|
|
|
convert_btn = ttk.Button(btn_frame, text="立即转换", command=convert_unicode)
|
|
convert_btn.pack(side="left", padx=10)
|
|
|
|
copy_btn = ttk.Button(btn_frame, text="复制结果", command=copy_to_clipboard)
|
|
copy_btn.pack(side="left")
|
|
|
|
# 窗口居中
|
|
root.eval('tk::PlaceWindow . center')
|
|
|
|
root.mainloop() |