Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_super_textbox_multiselect.py
- import tkinter as tk
- from collections import deque
- def do_toggle(i):
- if i in toggle:
- text.tag_remove("highlight", f"{i}.0", f"{i+1}.0")
- toggle.remove(i)
- else:
- text.tag_add("highlight", f"{i}.0", f"{i+1}.0")
- toggle.append(i)
- def line_idx(event):
- return int(text.index(f"@0, {event.y}").split(".")[0])
- def on_drag(event):
- i = line_idx(event)
- if i != prev_2_lines[1]:
- if i in prev_2_lines:
- do_toggle(prev_2_lines[1])
- do_toggle(i)
- prev_2_lines.append(i)
- def on_b1press(event):
- i = line_idx(event)
- do_toggle(i)
- prev_2_lines.clear()
- prev_2_lines.extend([-1, i])
- root = tk.Tk()
- items = [f"Item {i+1}" for i in range(30)]
- toggle = []
- prev_2_lines = deque(maxlen=2)
- prev_2_lines.extend([-1, -1])
- text = tk.Text(root, wrap=tk.NONE)
- text.bind("<B1-Motion>", on_drag)
- text.bind("<Button-1>", on_b1press)
- for item in items:
- text.insert(tk.END, item + "\n")
- text.delete(text.index("end-1c linestart"), tk.END)
- scrollbar = tk.Scrollbar(root, orient="vertical", command=text.yview)
- text.configure(yscrollcommand=scrollbar.set)
- text.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
- scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
- text.tag_configure("highlight", foreground="black", background="light green")
- text.tag_configure("sel", foreground="black", background="white")
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement