|
发表于 2023-5-21 14:16:45
|
显示全部楼层
还可以加一个copy按钮复制处理后的文本:
- import tkinter as tk
- def process_text():
- input_text = text_entry.get("1.0", "end-1c") # 获取输入框的内容
- lines = input_text.split('\n') # 按行分割
- processed_lines = [line[4:] if line.startswith(('>>> ', '... ')) else line for line in lines] # 去掉特定前缀
- processed_text = '\n'.join(processed_lines) # 重新连接成字符串
- text_display.delete("1.0", "end") # 清空显示框
- text_display.insert("end", processed_text) # 插入处理后的文本
- def copy_text():
- root.clipboard_clear() # 清除剪贴板
- text = text_display.get("1.0", "end-1c") # 获取显示框的内容
- root.clipboard_append(text) # 添加到剪贴板
- root = tk.Tk() # 创建主窗口
- root.title('Text Processor') # 设置窗口标题
- # 创建输入框
- text_entry = tk.Text(root, width=40, height=20)
- text_entry.pack(side="left")
- # 创建显示框
- text_display = tk.Text(root, width=40, height=20)
- text_display.pack(side="right")
- # 创建处理按钮
- process_button = tk.Button(root, text="Process", command=process_text)
- process_button.pack()
- # 创建复制按钮
- copy_button = tk.Button(root, text="Copy", command=copy_text)
- copy_button.pack()
- root.mainloop() # 开始主循环
复制代码
有什么需求可以继续提出来
|
|