Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_super_listbox_multiselect.py
- import tkinter as tk
- from collections import deque
- def do_toggle(i):
- if i in toggle:
- listbox.select_clear(i)
- toggle.remove(i)
- else:
- listbox.select_set(i)
- toggle.append(i)
- def on_drag(event):
- global prev_2_lines
- i = listbox.nearest(event.y)
- 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)
- 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])
- listbox = tk.Listbox(root, selectmode=tk.MULTIPLE)
- listbox.bind("<B1-Motion>", on_drag)
- for item in items:
- listbox.insert(tk.END, item)
- scrollbar = tk.Scrollbar(root, orient="vertical", command=listbox.yview)
- listbox.configure(yscrollcommand=scrollbar.set)
- listbox.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
- scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement