用python写一段代码,在windows上运行,使用tkinter,用于比较两段很长的文本content1和content2的差异,content1可通过按钮控件加载指定路径的文件,content2的内容由自定义函数func1给出。程序能够在可视化界面上展示差异项,展示的窗口分为左界面和右界面,左右界面有各自的水平滚动条控件,左右界面共用一个垂直滚动条控件,左界面显示content1,右界面显示content2,左界面和右界面内容中,存在差异的部分,用颜色进行标记。窗体大小能够适应控件。
时间: 2024-03-13 13:47:04 浏览: 61
好的,以下是符合您要求的代码:
```python
import tkinter as tk
from difflib import SequenceMatcher
class TextCompare:
def __init__(self, root):
self.root = root
self.root.title("文本比较工具")
self.root.geometry("800x600")
self.content1 = ""
self.content2 = ""
self.diff_indices = []
# 左界面
self.left_frame = tk.Frame(self.root)
self.left_frame.pack(side="left", fill="both", expand=True)
self.left_scrollbar_y = tk.Scrollbar(self.left_frame, orient="vertical")
self.left_scrollbar_y.pack(side="right", fill="y")
self.left_scrollbar_x = tk.Scrollbar(self.left_frame, orient="horizontal")
self.left_scrollbar_x.pack(side="bottom", fill="x")
self.text1 = tk.Text(self.left_frame, wrap="none", yscrollcommand=self.left_scrollbar_y.set, xscrollcommand=self.left_scrollbar_x.set)
self.text1.pack(side="left", fill="both", expand=True)
self.left_scrollbar_y.config(command=self.text1.yview)
self.left_scrollbar_x.config(command=self.text1.xview)
# 右界面
self.right_frame = tk.Frame(self.root)
self.right_frame.pack(side="left", fill="both", expand=True)
self.right_scrollbar_y = tk.Scrollbar(self.right_frame, orient="vertical")
self.right_scrollbar_y.pack(side="right", fill="y")
self.right_scrollbar_x = tk.Scrollbar(self.right_frame, orient="horizontal")
self.right_scrollbar_x.pack(side="bottom", fill="x")
self.text2 = tk.Text(self.right_frame, wrap="none", yscrollcommand=self.right_scrollbar_y.set, xscrollcommand=self.right_scrollbar_x.set)
self.text2.pack(side="left", fill="both", expand=True)
self.right_scrollbar_y.config(command=self.text2.yview)
self.right_scrollbar_x.config(command=self.text2.xview)
# 控制面板
self.control_frame = tk.Frame(self.root)
self.control_frame.pack(side="top", fill="x")
self.load_button = tk.Button(self.control_frame, text="加载文件", command=self.load_file)
self.load_button.pack(side="left", padx=10, pady=10)
self.compare_button = tk.Button(self.control_frame, text="比较", command=self.compare)
self.compare_button.pack(side="left", padx=10, pady=10)
def load_file(self):
file_path = tk.filedialog.askopenfilename()
if file_path:
with open(file_path, "r", encoding="utf-8") as f:
self.content1 = f.read()
self.text1.delete(1.0, "end")
self.text1.insert("end", self.content1)
def compare(self):
self.content2 = self.func1()
sm = SequenceMatcher(None, self.content1, self.content2)
self.diff_indices = sm.get_opcodes()[1:]
self.text2.delete(1.0, "end")
self.text2.insert("end", self.content2)
self.highlight_diff()
def highlight_diff(self):
for tag in self.text2.tag_names():
self.text2.tag_delete(tag)
for diff in self.diff_indices:
if diff[0] == "equal":
continue
elif diff[0] == "delete":
start = diff[1]
end = diff[2]
self.text1.tag_add("delete", f"{start}.0", f"{end}.0")
self.text1.tag_config("delete", foreground="red")
elif diff[0] == "insert":
start = diff[3]
end = diff[4]
self.text2.tag_add("insert", f"{start}.0", f"{end}.0")
self.text2.tag_config("insert", foreground="green")
elif diff[0] == "replace":
start = diff[1]
end = diff[2]
self.text1.tag_add("replace", f"{start}.0", f"{end}.0")
self.text1.tag_config("replace", foreground="blue")
start = diff[3]
end = diff[4]
self.text2.tag_add("replace", f"{start}.0", f"{end}.0")
self.text2.tag_config("replace", foreground="blue")
def func1():
# 自定义函数,返回文本内容
return "这是content2的内容"
root = tk.Tk()
app = TextCompare(root)
app.func1 = func1
root.mainloop()
```
这段代码会创建一个可视化窗口,其中包含两个文本框和一些按钮控件。通过点击"加载文件"按钮,可以加载指定路径的文件到左界面中。点击"比较"按钮,程序会调用自定义函数`func1()`获取`content2`的内容,然后使用`difflib`库计算出`content1`和`content2`之间的差异,最后将差异项在右界面中用颜色标记出来。
阅读全文