Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_QuickCompare.py
- # inspired by github's version
- import tkinter as tk
- from tkinter import messagebox
- root = tk.Tk()
- root.geometry("640x640+0+0")
- root.title("QuickCompare")
- text_area = tk.Text(root, wrap="none")
- text_area.pack(expand=True, fill="both")
- def update_colors():
- text_area.tag_configure("doc1_selected", background="white")
- text_area.tag_configure("doc2_not_selected", background="light green")
- text_area.tag_configure("doc1_removed", background="light pink")
- for i, tag in enumerate(selection_idx):
- text_area.tag_add(tag, f"{i + 1}.0", f"{i + 1}.end + 1c")
- def paste_doc2():
- global selection_idx
- try:
- doc1_lines = text_area.get(1.0, tk.END).strip().split('\n')
- doc2_lines = root.clipboard_get().strip().split('\n')
- merged_lines = []
- selection_idx = []
- doc1_set = set(doc1_lines)
- doc2_set = set(doc2_lines)
- for line in doc1_lines:
- if line in doc2_set:
- merged_lines.append(line)
- selection_idx.append("doc1_selected")
- else:
- merged_lines.append(line)
- selection_idx.append("doc1_removed")
- doc2_index = 0
- for i, line in enumerate(merged_lines):
- while doc2_index < len(doc2_lines) and doc2_lines[doc2_index] not in doc1_set:
- merged_lines.insert(i, doc2_lines[doc2_index])
- selection_idx.insert(i, "doc2_not_selected")
- doc2_index += 1
- i += 1
- if doc2_index < len(doc2_lines) and doc2_lines[doc2_index] == line:
- doc2_index += 1
- while doc2_index < len(doc2_lines):
- merged_lines.append(doc2_lines[doc2_index])
- selection_idx.append("doc2_not_selected")
- doc2_index += 1
- text_area.delete(1.0, tk.END)
- text_area.insert(tk.END, '\n'.join(merged_lines))
- update_colors()
- except tk.TclError:
- messagebox.showerror("Error", "Clipboard Is Empty Or Not Accessible.")
- # Demo
- doc1 = """
- print("Hello, World!")
- x = 1
- y = 2
- z = x + y
- print(z)
- a = 3
- b = 4
- c = a + b
- print(c)
- # doc1
- d = 5
- e = 6
- f = d + e
- print(f)""".strip()
- doc2 = """
- # demo
- print("Hello, World!")
- x = 1
- y = 2
- z = x + y
- print(z)
- def bar():
- a = 3
- b = 4
- c = a + b
- print(c)
- # doc1
- # doc2
- d = 5
- e = 6
- f = d + e
- print(f)
- bar()"""
- text_area.delete(1.0, tk.END)
- text_area.insert(tk.END, doc1)
- root.clipboard_clear()
- root.clipboard_append(doc2)
- paste_button = tk.Button(root, text="Paste Doc2", command=paste_doc2)
- paste_button.pack()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement