Advertisement
here2share

# tk_super_listbox_multiselect.py

Sep 6th, 2024 (edited)
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.94 KB | None | 0 0
  1. # tk_super_listbox_multiselect.py
  2.  
  3. import tkinter as tk
  4. from collections import deque
  5.  
  6. def do_toggle(i):
  7.     if i in toggle:
  8.         listbox.select_clear(i)
  9.         toggle.remove(i)
  10.     else:
  11.         listbox.select_set(i)
  12.         toggle.append(i)
  13.  
  14. def on_drag(event):
  15.     global prev_2_lines
  16.     i = listbox.nearest(event.y)
  17.     if i != prev_2_lines[1]:
  18.         if i in prev_2_lines:
  19.             do_toggle(prev_2_lines[1])
  20.         do_toggle(i)
  21.         prev_2_lines.append(i)
  22.            
  23. root = tk.Tk()
  24.  
  25. items = [f"Item {i+1}" for i in range(30)]
  26.  
  27. toggle = []
  28. prev_2_lines = deque(maxlen=2)
  29. prev_2_lines.extend([-1, -1])
  30.  
  31. listbox = tk.Listbox(root, selectmode=tk.MULTIPLE)
  32. listbox.bind("<B1-Motion>", on_drag)
  33.  
  34. for item in items:
  35.     listbox.insert(tk.END, item)
  36.  
  37. scrollbar = tk.Scrollbar(root, orient="vertical", command=listbox.yview)
  38. listbox.configure(yscrollcommand=scrollbar.set)
  39.  
  40. listbox.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
  41. scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
  42.  
  43. root.mainloop()
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement