94 lines
3.8 KiB
Python
94 lines
3.8 KiB
Python
import tkinter as tk
|
||
from tkinter import ttk
|
||
from tkinter import messagebox
|
||
|
||
def create_ui(parent, run_callback):
|
||
"""创建前端界面:两个输入框、数值比较勾选框、分隔符选择、忽略微小差异选项、输出框、运行和清空按钮"""
|
||
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(5, weight=1)
|
||
|
||
ttk.Label(frame, text="比较两个字符串的元素差异").grid(
|
||
row=0, column=0, columnspan=2, sticky=tk.W, pady=5
|
||
)
|
||
|
||
ttk.Label(frame, text="字符串1:").grid(row=1, column=0, sticky=tk.W, pady=2)
|
||
input_entry1 = tk.Text(frame, height=4, width=50)
|
||
input_entry1.grid(row=1, column=1, sticky=(tk.W, tk.E), pady=2)
|
||
|
||
ttk.Label(frame, text="字符串2:").grid(row=2, column=0, sticky=tk.W, pady=2)
|
||
input_entry2 = tk.Text(frame, height=4, width=50)
|
||
input_entry2.grid(row=2, column=1, sticky=(tk.W, tk.E), pady=2)
|
||
|
||
ttk.Label(frame, text="分隔符:").grid(row=3, column=0, sticky=tk.W, pady=2)
|
||
separator_var = tk.StringVar(value=",")
|
||
separator_menu = ttk.OptionMenu(
|
||
frame,
|
||
separator_var,
|
||
",",
|
||
",", ";", "\\n", "空格"
|
||
)
|
||
separator_menu.grid(row=3, column=1, sticky=tk.W, pady=2)
|
||
|
||
numeric_var = tk.BooleanVar(value=False)
|
||
ttk.Checkbutton(frame, text="启用数值比较", variable=numeric_var).grid(
|
||
row=3, column=1, sticky=tk.E, pady=2
|
||
)
|
||
|
||
ignore_tiny_diff_var = tk.BooleanVar(value=False)
|
||
ignore_tiny_diff_check = ttk.Checkbutton(
|
||
frame, text="忽略微小差异", variable=ignore_tiny_diff_var,
|
||
command=lambda: numeric_var.set(True) if ignore_tiny_diff_var.get() else None
|
||
)
|
||
ignore_tiny_diff_check.grid(row=4, column=1, sticky=tk.E, pady=2)
|
||
|
||
ttk.Label(frame, text="比较结果:").grid(row=5, column=0, sticky=tk.NW, pady=2)
|
||
output_text = tk.Text(frame, height=4, width=50, state="disabled")
|
||
output_text.grid(row=5, column=1, 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=2, sticky=(tk.N, tk.S))
|
||
output_text["yscrollcommand"] = output_scroll.set
|
||
|
||
def run():
|
||
input1 = input_entry1.get("1.0", tk.END).strip()
|
||
input2 = input_entry2.get("1.0", tk.END).strip()
|
||
if not input1 or not input2:
|
||
messagebox.showerror("错误", "请输入两个字符串")
|
||
return
|
||
separator = separator_var.get()
|
||
if separator == "空格":
|
||
separator = " "
|
||
try:
|
||
input_list = [
|
||
input1,
|
||
input2,
|
||
separator,
|
||
str(numeric_var.get()).lower(),
|
||
str(ignore_tiny_diff_var.get()).lower()
|
||
]
|
||
result = run_callback(input_list)
|
||
output_text.configure(state="normal")
|
||
output_text.delete("1.0", tk.END)
|
||
if result and result[0]:
|
||
output_text.insert("1.0", result[0])
|
||
else:
|
||
output_text.insert("1.0", "无结果")
|
||
output_text.configure(state="disabled")
|
||
except Exception as e:
|
||
messagebox.showerror("错误", f"运行脚本失败:{str(e)}")
|
||
|
||
def clear():
|
||
input_entry1.delete("1.0", tk.END)
|
||
input_entry2.delete("1.0", tk.END)
|
||
output_text.configure(state="normal")
|
||
output_text.delete("1.0", tk.END)
|
||
output_text.configure(state="disabled")
|
||
numeric_var.set(False)
|
||
ignore_tiny_diff_var.set(False)
|
||
separator_var.set(",")
|
||
|
||
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 |