how can i add auto-hide scrollbars with python tkinter in a listbox? for example, to hide the scrollbar when the size of the listbox is less than or equal to the size of the frame, and otherwise reappear.
I wrote a code that creates me a listbox with scrollboxes. It is fully working, now I would like to add the ability to hide scrollboxes.
import tkinter
from tkinter import ttk
class ScrollingListbox(ttk.Frame):
def __init__(self, master, width=80, selectmode="multiple", borderwidth=0, highlightthickness=0):
ttk.Frame.__init__(self, master)
self._scrollbar_vertical = ttk.Scrollbar(self)
self._scrollbar_vertical.pack(side=tkinter.RIGHT, fill=tkinter.Y)
self._scrollbar_horizontal = ttk.Scrollbar(self, orient="horizontal")
self._scrollbar_horizontal.pack(side=tkinter.BOTTOM, fill=tkinter.X)
self._listbox_playlist_tracks = tkinter.Listbox(self, width=width, selectmode=selectmode,
yscrollcommand=self._scrollbar_vertical.set,
xscrollcommand=self._scrollbar_horizontal.set,
borderwidth=borderwidth, highlightthickness=highlightthickness)
self._listbox_playlist_tracks.pack(expand=1, fill=tkinter.BOTH, padx=5, pady=5)
self._scrollbar_vertical.config(command=self._listbox_playlist_tracks.yview)
self._scrollbar_horizontal.config(command=self._listbox_playlist_tracks.xview)
def insert(self, index=tkinter.END, value=0):
self._listbox_playlist_tracks.insert(index, value)
def delete(self, first=0, last=tkinter.END):
self._listbox_playlist_tracks.delete(first, last)
def foo(self):
new_height = self.winfo_height()
min_height = self._listbox_playlist_tracks.winfo_reqheight()
print(f'f={new_height}, l={min_height}')
if __name__ == '__main__':
root = tkinter.Tk()
root.geometry('700x500')
_listbox_playlist_tracks = ScrollingListbox(root)
_listbox_playlist_tracks.pack(expand=1, fill=tkinter.BOTH, padx=5, pady=5)
for i in range(500):
_listbox_playlist_tracks.insert(tkinter.END, i)
_listbox_playlist_tracks.foo()
I tried to implement it with similar methods as in this implementation, but the winfo_reqwidth() method constantly produces the same value. Perhaps there are some other methods for getting the dimensions of the listbox and comparing it with the dimensions of the parent frame.
another example for canvas i found here: https://www.geeksforgeeks.org/autohiding-scrollbars-using-python-tkinter/