111 lines
4.8 KiB
Python
111 lines
4.8 KiB
Python
import tkinter as tk
|
||
from tkinter import ttk
|
||
import pyperclip
|
||
from tkinter import messagebox
|
||
import json
|
||
|
||
def create_ui(parent, run_callback):
|
||
"""创建前端界面:JSON 输入、字段选择、模板输入、输出显示"""
|
||
frame = ttk.Frame(parent)
|
||
frame.grid(row=0, column=0, sticky=(tk.W, tk.E, tk.N, tk.S))
|
||
frame.columnconfigure(1, weight=1)
|
||
frame.rowconfigure(1, weight=1)
|
||
frame.rowconfigure(4, weight=1)
|
||
|
||
# 描述
|
||
ttk.Label(frame, text="解析 JSON 数组,按模板拼接指定字段(如 $field$:OJ:$field2$)").grid(
|
||
row=0, column=0, columnspan=3, sticky=tk.W, pady=5
|
||
)
|
||
|
||
# JSON 输入
|
||
ttk.Label(frame, text="输入 JSON:").grid(row=1, column=0, sticky=tk.NW, pady=2)
|
||
input_text = tk.Text(frame, height=5, width=50)
|
||
input_text.grid(row=1, column=1, columnspan=2, sticky=(tk.W, tk.E, tk.N, tk.S), pady=2)
|
||
input_scroll = ttk.Scrollbar(frame, orient=tk.VERTICAL, command=input_text.yview)
|
||
input_scroll.grid(row=1, column=3, sticky=(tk.N, tk.S))
|
||
input_text["yscrollcommand"] = input_scroll.set
|
||
|
||
# 字段选择
|
||
ttk.Label(frame, text="可用字段:").grid(row=2, column=0, sticky=tk.NW, pady=2)
|
||
field_listbox = tk.Listbox(frame, height=5, selectmode=tk.SINGLE)
|
||
field_listbox.grid(row=2, column=1, sticky=(tk.W, tk.E, tk.N, tk.S), pady=2)
|
||
field_scroll = ttk.Scrollbar(frame, orient=tk.VERTICAL, command=field_listbox.yview)
|
||
field_scroll.grid(row=2, column=2, sticky=(tk.N, tk.S))
|
||
field_listbox["yscrollcommand"] = field_scroll.set
|
||
|
||
def detect_fields():
|
||
"""检测 JSON 中的字段"""
|
||
field_listbox.delete(0, tk.END)
|
||
json_str = input_text.get("1.0", tk.END).strip()
|
||
try:
|
||
data = json.loads(json_str)
|
||
if isinstance(data, dict) and "data" in data and isinstance(data["data"], list) and data["data"]:
|
||
fields = set(data["data"][0].keys())
|
||
for field in sorted(fields):
|
||
field_listbox.insert(tk.END, field)
|
||
else:
|
||
messagebox.showerror("错误", "JSON 格式无效或缺少 'data' 数组")
|
||
except json.JSONDecodeError:
|
||
messagebox.showerror("错误", "无效的 JSON 字符串")
|
||
except Exception as e:
|
||
messagebox.showerror("错误", f"检测失败:{str(e)}")
|
||
|
||
def add_to_template():
|
||
"""将选中的字段添加到模板"""
|
||
selected = field_listbox.curselection()
|
||
if not selected:
|
||
messagebox.showwarning("警告", "请选择一个字段")
|
||
return
|
||
field = field_listbox.get(selected[0])
|
||
template_entry.insert(tk.END, f"${field}$")
|
||
|
||
ttk.Button(frame, text="检测字段", command=detect_fields).grid(row=3, column=1, sticky=tk.W, pady=2)
|
||
ttk.Button(frame, text="添加到模板", command=add_to_template).grid(row=3, column=1, sticky=tk.E, pady=2)
|
||
|
||
# 模板输入
|
||
ttk.Label(frame, text="拼接模板:").grid(row=4, column=0, sticky=tk.NW, pady=2)
|
||
template_entry = ttk.Entry(frame)
|
||
template_entry.grid(row=4, column=1, columnspan=2, sticky=(tk.W, tk.E), pady=2)
|
||
|
||
# 输出
|
||
ttk.Label(frame, text="输出结果:").grid(row=5, column=0, sticky=tk.NW, pady=2)
|
||
output_text = tk.Text(frame, height=5, width=50, state="disabled")
|
||
output_text.grid(row=5, column=1, columnspan=2, sticky=(tk.W, tk.E, tk.N, tk.S), pady=2)
|
||
output_scroll = ttk.Scrollbar(frame, orient=tk.VERTICAL, command=output_text.yview)
|
||
output_scroll.grid(row=5, column=3, sticky=(tk.N, tk.S))
|
||
output_text["yscrollcommand"] = output_scroll.set
|
||
|
||
def copy_output(event):
|
||
output = output_text.get("1.0", tk.END).strip()
|
||
if output:
|
||
pyperclip.copy(output)
|
||
messagebox.showinfo("提示", "输出已复制到剪贴板")
|
||
|
||
output_text.bind("<Button-1>", copy_output)
|
||
|
||
# 运行按钮
|
||
def run():
|
||
json_str = input_text.get("1.0", tk.END).strip()
|
||
template = template_entry.get().strip()
|
||
if not json_str or not template:
|
||
messagebox.showerror("错误", "请输入 JSON 和模板")
|
||
return
|
||
result = run_callback([json_str, template])
|
||
output_text.configure(state="normal")
|
||
output_text.delete("1.0", tk.END)
|
||
output_text.insert("1.0", result[0] if result else "无结果")
|
||
output_text.configure(state="disabled")
|
||
|
||
# 清空按钮
|
||
def clear():
|
||
input_text.delete("1.0", tk.END)
|
||
template_entry.delete(0, tk.END)
|
||
field_listbox.delete(0, tk.END)
|
||
output_text.configure(state="normal")
|
||
output_text.delete("1.0", tk.END)
|
||
output_text.configure(state="disabled")
|
||
|
||
ttk.Button(frame, text="运行脚本", command=run).grid(row=6, column=0, pady=5)
|
||
ttk.Button(frame, text="清空", command=clear).grid(row=6, column=1, sticky=tk.W, pady=5)
|
||
|
||
return frame |