Advertisement
here2share

# tk_custom_scrollbar.py

Sep 20th, 2024
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.20 KB | None | 0 0
  1. # tk_custom_scrollbar.py
  2.  
  3. import tkinter as tk
  4.  
  5. def custom_scrollbar(master, target, orient=tk.VERTICAL, width=10):
  6.     def set_scrollbar(scrollbar, low, high):
  7.         low = float(low)
  8.         high = float(high)
  9.         cv.thumb_position = low
  10.         cv.thumb_size = high - low
  11.         redraw_scrollbar()
  12.  
  13.     def redraw_scrollbar():
  14.         cv.delete("sbar")
  15.         if cv.orient == tk.VERTICAL:
  16.             thumb_height = cv.winfo_height() * cv.thumb_size
  17.             thumb_y = cv.winfo_height() * cv.thumb_position
  18.             cv.create_rectangle(0, thumb_y, cv.winfo_width(), thumb_y + thumb_height, fill="gray", tag="sbar")
  19.         else:
  20.             thumb_width = cv.winfo_width() * cv.thumb_size
  21.             thumb_x = cv.winfo_width() * cv.thumb_position
  22.             cv.create_rectangle(thumb_x, 0, thumb_x + thumb_width, cv.winfo_height(), fill="gray", tag="sbar")
  23.  
  24.     def on_press(event):
  25.         if cv.orient == tk.VERTICAL:
  26.             cv.dragging = event.y
  27.         else:
  28.             cv.dragging = event.x
  29.         update_command()
  30.  
  31.     def on_motion(event):
  32.         if cv.dragging is not False:
  33.             if cv.orient == tk.VERTICAL:
  34.                 delta = event.y - cv.dragging
  35.                 cv.dragging = event.y
  36.                 cv.thumb_position += delta / cv.winfo_height()
  37.             else:
  38.                 delta = event.x - cv.dragging
  39.                 cv.dragging = event.x
  40.                 cv.thumb_position += delta / cv.winfo_width()
  41.             cv.thumb_position = max(0, min(1 - cv.thumb_size, cv.thumb_position))
  42.             redraw_scrollbar()
  43.             update_command()
  44.  
  45.     def on_release(event):
  46.         cv.dragging = False
  47.  
  48.     def update_command():
  49.         cmd("moveto", cv.thumb_position)
  50.  
  51.     cmd = target.yview
  52.     cv = tk.Canvas(master, width=width)
  53.     cv.orient = orient  # Set the orient attribute
  54.     cv.bind("<ButtonPress-1>", lambda event: on_press(event))
  55.     cv.bind("<B1-Motion>", lambda event: on_motion(event))
  56.     cv.bind("<ButtonRelease-1>", lambda event: on_release(event))
  57.     cv.dragging = False
  58.     cv.thumb_position = 0.0
  59.     cv.thumb_size = 0.0
  60.     target.config(yscrollcommand=lambda low, high: set_scrollbar(scrollbar, low, high))
  61.     return cv
  62.  
  63. root = tk.Tk()
  64.  
  65. listbox = tk.Listbox(root, height=10, width=20)
  66. listbox.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
  67.  
  68. scrollbar = custom_scrollbar(root, listbox, orient=tk.VERTICAL)
  69. scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
  70.  
  71. for i in range(100):
  72.     listbox.insert(tk.END, f"Item {i}")
  73.  
  74. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement