Skip to content

Commit a74f777

Browse files
committed
Factor out common scrollbar construction code
1 parent de045d8 commit a74f777

File tree

1 file changed

+9
-6
lines changed
  • src/ScrollableContainers

1 file changed

+9
-6
lines changed

src/ScrollableContainers/_tk.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import platform
44
import tkinter as tk
5+
from collections.abc import Callable
56
from tkinter import ttk
67

78
_system = platform.system()
@@ -19,13 +20,9 @@ def __init__(self, *args, **kwargs):
1920

2021
# Using the grid geometry manager ensures that the horizontal and
2122
# vertical scrollbars do not touch.
22-
self._xscrollbar = ttk.Scrollbar(self, orient=tk.HORIZONTAL, command=self._xview)
23-
self._xscrollbar.bind("<Enter>", self._cancel_hide_scrollbars)
24-
self._xscrollbar.bind("<Leave>", self._schedule_hide_scrollbars)
23+
self._xscrollbar = self._scrollbar(tk.HORIZONTAL, self._xview)
2524
self._xscrollbar.grid(row=1, column=0, sticky=tk.EW)
26-
self._yscrollbar = ttk.Scrollbar(self, orient=tk.VERTICAL, command=self._yview)
27-
self._yscrollbar.bind("<Enter>", self._cancel_hide_scrollbars)
28-
self._yscrollbar.bind("<Leave>", self._schedule_hide_scrollbars)
25+
self._yscrollbar = self._scrollbar(tk.VERTICAL, self._yview)
2926
self._yscrollbar.grid(row=0, column=1, sticky=tk.NS)
3027
self._hide_scrollbars_id = None
3128

@@ -56,6 +53,12 @@ def __init__(self, *args, **kwargs):
5653
self._canvas.xview_moveto(0.0)
5754
self._canvas.yview_moveto(0.0)
5855

56+
def _scrollbar(self, orient: str, command: Callable[..., None]) -> ttk.Scrollbar:
57+
scrollbar = ttk.Scrollbar(self, orient=orient, command=command)
58+
scrollbar.bind("<Enter>", self._cancel_hide_scrollbars)
59+
scrollbar.bind("<Leave>", self._schedule_hide_scrollbars)
60+
return scrollbar
61+
5962
@property
6063
def frame(self) -> ttk.Frame:
6164
return self._frame

0 commit comments

Comments
 (0)