From baaac161b2592d363d1110eda9c09247df99faab Mon Sep 17 00:00:00 2001 From: Vishal Pankaj Chandratreya <19171016+tfpf@users.noreply.github.com> Date: Sat, 24 Aug 2024 16:51:57 +0530 Subject: [PATCH] Show scrollbars only when in use --- src/ScrollableContainers/_tk.py | 47 ++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/src/ScrollableContainers/_tk.py b/src/ScrollableContainers/_tk.py index 749fb0b..64befaf 100644 --- a/src/ScrollableContainers/_tk.py +++ b/src/ScrollableContainers/_tk.py @@ -17,20 +17,22 @@ class ScrollableFrameTk(ttk.Frame): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) + # Using the grid geometry manager ensures that the horizontal and + # vertical scrollbars do not touch. + self._xscrollbar = ttk.Scrollbar(self, orient=tk.HORIZONTAL, command=self._xview) + self._xscrollbar.grid(row=1, column=0, sticky=tk.EW) + self._yscrollbar = ttk.Scrollbar(self, orient=tk.VERTICAL, command=self._yview) + self._yscrollbar.grid(row=0, column=1, sticky=tk.NS) + # Scrollable canvas. This is the widget which actually manages - # scrolling. Using the grid geometry manager ensures that the - # horizontal and vertical scrollbars do not meet. + # scrolling. Initially, it will be above the scrollbars, so the latter + # won't be visible. self._canvas = tk.Canvas(self) self._canvas.bind("", self._on_canvas_configure) self._canvas.bind("", self._on_canvas_enter) self._canvas.bind("", self._on_canvas_leave) - self._canvas.grid(row=0, column=0, sticky=tk.NSEW) - - xscrollbar = ttk.Scrollbar(self, orient=tk.HORIZONTAL, command=self._xview) - xscrollbar.grid(row=1, column=0, sticky=tk.EW) - yscrollbar = ttk.Scrollbar(self, orient=tk.VERTICAL, command=self._yview) - yscrollbar.grid(row=0, column=1, sticky=tk.NS) - self._canvas.configure(xscrollcommand=xscrollbar.set, yscrollcommand=yscrollbar.set) + self._canvas.configure(xscrollcommand=self._xscrollbar.set, yscrollcommand=self._yscrollbar.set) + self._canvas.grid(row=0, column=0, rowspan=2, columnspan=2, sticky=tk.NSEW) self.grid_rowconfigure(0, weight=1) self.grid_columnconfigure(0, weight=1) @@ -46,6 +48,32 @@ def __init__(self, *args, **kwargs): self._canvas.xview_moveto(0.0) self._canvas.yview_moveto(0.0) + def _show_scrollbars(self): + """ + Move the horizontal and vertical scrollbars above the scrollable + canvas. + """ + self._xscrollbar.lift() + self._yscrollbar.lift() + + def _hide_scrollbars(self): + """ + Move the horizontal and vertical scrollbars below the scrollable + canvas. + """ + self._xscrollbar.lower() + self._yscrollbar.lower() + + def _peek_scrollbars(self, ms=1000): + """ + Show the horizontal and vertical scrollbars, and hide them after a + delay. + + :param ms: Delay in milliseconds. + """ + self._show_scrollbars() + self.after(ms, self._hide_scrollbars) + def _xview(self, *args, width: int | None = None): """ Called when a horizontal scroll is requested. Called by other callbacks @@ -165,3 +193,4 @@ def _on_mouse_scroll(self, event: tk.Event): case _: message = f"event {event.num} on OS {_system!r} is not supported" raise ValueError(message) + self._peek_scrollbars()