jb_fr/scripts/多个字符串拼接为一个.ui.py

144 lines
6.1 KiB
Python
Raw Permalink Normal View History

2025-06-06 21:04:04 +08:00
import tkinter as tk
from tkinter import ttk
import pyperclip
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(0, weight=1)
frame.rowconfigure(1, weight=1)
# 描述
ttk.Label(frame, text="将多个字符串按位置拼接为 str1:str2:str3,...,支持自定义输入分隔符和输出分隔符(如 \\n 表示换行)").grid(
row=0, column=0, columnspan=3, sticky=tk.W, pady=5
)
# 可滚动区域,用于输入框
canvas = tk.Canvas(frame)
canvas.grid(row=1, column=0, columnspan=3, sticky=(tk.W, tk.E, tk.N, tk.S))
scrollbar = ttk.Scrollbar(frame, orient=tk.VERTICAL, command=canvas.yview)
scrollbar.grid(row=1, column=3, sticky=(tk.N, tk.S))
canvas.configure(yscrollcommand=scrollbar.set)
# 内部框架,包含输入框
inner_frame = ttk.Frame(canvas)
canvas_frame_id = canvas.create_window((0, 0), window=inner_frame, anchor=tk.NW)
def update_scrollregion(event=None):
"""更新滚动区域"""
canvas.configure(scrollregion=canvas.bbox("all"))
canvas.itemconfig(canvas_frame_id, width=canvas.winfo_width())
inner_frame.bind("<Configure>", update_scrollregion)
# 存储动态控件
input_texts = []
input_labels = []
input_separator_entry = None
output_separator_entry = None
output_label = ttk.Label(frame, text="输出结果:")
output_text = tk.Text(frame, height=4, width=50, state="disabled")
output_scroll = ttk.Scrollbar(frame, orient=tk.VERTICAL, command=output_text.yview)
add_button = None
run_button = None
clear_button = None
def add_input_field():
"""添加新的多行输入框,无数量限制"""
nonlocal output_label, output_text, output_scroll, add_button, run_button, clear_button, input_separator_entry, output_separator_entry
row = len(input_texts)
label = ttk.Label(inner_frame, text=f"字符串{row + 1}")
label.grid(row=row, column=0, sticky=tk.NW, pady=2)
text = tk.Text(inner_frame, height=3, width=50)
text.grid(row=row, column=1, columnspan=2, sticky=(tk.W, tk.E), pady=2)
input_labels.append(label)
input_texts.append(text)
inner_frame.columnconfigure(1, weight=1)
# 更新滚动区域
update_scrollregion()
# 初始化两个输入框
for _ in range(2):
add_input_field()
# 输入分隔符
input_separator_label = ttk.Label(frame, text="输入分隔符(\\n 表示换行):")
input_separator_label.grid(row=2, column=0, sticky=tk.W, pady=2)
input_separator_entry = ttk.Entry(frame, width=5)
input_separator_entry.insert(0, ",") # 默认逗号
input_separator_entry.grid(row=2, column=1, sticky=tk.W, pady=2)
# 输出分隔符
output_separator_label = ttk.Label(frame, text="输出分隔符(\\n 表示换行):")
output_separator_label.grid(row=3, column=0, sticky=tk.W, pady=2)
output_separator_entry = ttk.Entry(frame, width=5)
output_separator_entry.insert(0, ",") # 默认逗号
output_separator_entry.grid(row=3, column=1, sticky=tk.W, pady=2)
# 输出框配置
output_label.grid(row=4, column=0, sticky=tk.NW, pady=2)
output_text.grid(row=4, column=1, sticky=(tk.W, tk.E, tk.N, tk.S), pady=2)
output_scroll.grid(row=4, column=2, 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)
# 添加字符串按钮
add_button = ttk.Button(frame, text="添加字符串", command=add_input_field)
add_button.grid(row=5, column=0, pady=5)
# 运行按钮
def run():
inputs = [text.get("1.0", tk.END).rstrip("\n") for text in input_texts]
input_separator = input_separator_entry.get().strip()
output_separator = output_separator_entry.get().strip()
if len(inputs) < 2 or not all(inputs):
messagebox.showerror("错误", "请至少输入两个非空字符串")
return
if not input_separator or not output_separator:
messagebox.showerror("错误", "请输入输入和输出分隔符")
return
# 传递字符串和分隔符
result = run_callback(inputs + [input_separator, output_separator])
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():
nonlocal output_label, output_text, output_scroll, add_button, run_button, clear_button, input_separator_entry, output_separator_entry
# 移除多余输入框,保留两个
while len(input_texts) > 2:
input_texts.pop().destroy()
input_labels.pop().destroy()
# 清空输入和输出
for text in input_texts:
text.delete("1.0", tk.END)
input_separator_entry.delete(0, tk.END)
input_separator_entry.insert(0, ",") # 重置输入分隔符
output_separator_entry.delete(0, tk.END)
output_separator_entry.insert(0, ",") # 重置输出分隔符
output_text.configure(state="normal")
output_text.delete("1.0", tk.END)
output_text.configure(state="disabled")
# 重置布局
for i, (label, text) in enumerate(zip(input_labels, input_texts)):
label.grid(row=i, column=0, sticky=tk.NW, pady=2)
text.grid(row=i, column=1, columnspan=2, sticky=(tk.W, tk.E), pady=2)
update_scrollregion()
run_button = ttk.Button(frame, text="运行脚本", command=run)
run_button.grid(row=6, column=0, pady=5)
clear_button = ttk.Button(frame, text="清空", command=clear)
clear_button.grid(row=6, column=1, sticky=tk.W, pady=5)
return frame