|
6 | 6 | from tkinter import ttk
|
7 | 7 | from typing import Union
|
8 | 8 |
|
| 9 | +from TkZero import Platform |
| 10 | + |
9 | 11 |
|
10 | 12 | class Frame(ttk.Frame):
|
11 | 13 | def __init__(self, parent: Union[tk.Widget, Union[tk.Tk, tk.Toplevel]]):
|
@@ -145,3 +147,142 @@ def apply_style(self, style_name: str) -> None:
|
145 | 147 | f"(type passed in: {repr(type(style_name))})"
|
146 | 148 | )
|
147 | 149 | self.configure(style=f"{style_name}.{self._style_root}")
|
| 150 | + |
| 151 | + |
| 152 | +class ScrollableFrame(Frame): |
| 153 | + def __init__( |
| 154 | + self, |
| 155 | + parent: Union[tk.Widget, Union[tk.Tk, tk.Toplevel]], |
| 156 | + x_scrolling: bool = False, |
| 157 | + y_scrolling: bool = True, |
| 158 | + ): |
| 159 | + """ |
| 160 | + Create a scrollable frame. |
| 161 | + MAKE SURE WHEN GRIDING WIDGETS TO THIS FRAME TO USE THE FRAME |
| 162 | + ATTRIBUTE INSTEAD OF THIS FRAME DIRECTLY. |
| 163 | +
|
| 164 | + :param parent: The parent of this frame. |
| 165 | + :param x_scrolling: A bool on whether to add a scrollbar in the x |
| 166 | + direction. |
| 167 | + :param y_scrolling: A bool on whether to add a scrollbar in the y |
| 168 | + direction. |
| 169 | + """ |
| 170 | + # https://blog.teclado.com/tkinter-scrollable-frames/ |
| 171 | + super().__init__(parent) |
| 172 | + self.canvas = tk.Canvas(self) |
| 173 | + if x_scrolling: |
| 174 | + x_scrollbar = ttk.Scrollbar( |
| 175 | + self, orient=tk.HORIZONTAL, command=self.canvas.xview |
| 176 | + ) |
| 177 | + if y_scrolling: |
| 178 | + y_scrollbar = ttk.Scrollbar( |
| 179 | + self, orient=tk.VERTICAL, command=self.canvas.yview |
| 180 | + ) |
| 181 | + self.frame = Frame(self.canvas) |
| 182 | + self.frame.bind( |
| 183 | + "<Configure>", |
| 184 | + lambda _: self.canvas.configure(scrollregion=self.canvas.bbox(tk.ALL)), |
| 185 | + ) |
| 186 | + self.canvas.create_window((0, 0), window=self.frame, anchor=tk.NW) |
| 187 | + if x_scrolling: |
| 188 | + self.canvas.configure(xscrollcommand=x_scrollbar.set) |
| 189 | + if y_scrolling: |
| 190 | + self.canvas.configure(yscrollcommand=y_scrollbar.set) |
| 191 | + self.canvas.grid(row=0, column=0) |
| 192 | + if x_scrolling: |
| 193 | + x_scrollbar.grid(row=1, column=0, sticky=tk.NSEW) |
| 194 | + if y_scrolling: |
| 195 | + y_scrollbar.grid(row=0, column=1, sticky=tk.NSEW) |
| 196 | + # https://stackoverflow.com/a/37858368/10291933 |
| 197 | + self.frame.bind("<Enter>", self._bind_to_mousewheel) |
| 198 | + self.frame.bind("<Leave>", self._unbind_to_mousewheel) |
| 199 | + self._shift_pressed = False |
| 200 | + # https://stackoverflow.com/a/8089241/10291933 |
| 201 | + # http://www.tcl.tk/man/tcl8.4/TkCmd/keysyms.htm |
| 202 | + self.frame.bind_all("<Shift_L>", lambda _: self._set_shift_pressed(True)) |
| 203 | + self.frame.bind_all("<Shift_R>", lambda _: self._set_shift_pressed(True)) |
| 204 | + self.frame.bind_all( |
| 205 | + "<KeyRelease-Shift_L>", lambda _: self._set_shift_pressed(False) |
| 206 | + ) |
| 207 | + self.frame.bind_all( |
| 208 | + "<KeyRelease-Shift_R>", lambda _: self._set_shift_pressed(False) |
| 209 | + ) |
| 210 | + |
| 211 | + def _set_shift_pressed(self, is_pressed: bool) -> None: |
| 212 | + """ |
| 213 | + Set whether shift is pressed or not. |
| 214 | +
|
| 215 | + :param is_pressed: A bool. |
| 216 | + :return: None. |
| 217 | + """ |
| 218 | + self._shift_pressed = is_pressed |
| 219 | + |
| 220 | + def _bind_to_mousewheel(self, event) -> None: |
| 221 | + """ |
| 222 | + Bind to the mousewheel. |
| 223 | +
|
| 224 | + :param event: An event that Tkinter passes in. |
| 225 | + :return: None |
| 226 | + """ |
| 227 | + self.canvas.bind_all("<MouseWheel>", self._on_mousewheel) |
| 228 | + |
| 229 | + def _unbind_to_mousewheel(self, event) -> None: |
| 230 | + """ |
| 231 | + Unbind to the mousewheel. |
| 232 | +
|
| 233 | + :param event: An event that Tkinter passes in. |
| 234 | + :return: None |
| 235 | + """ |
| 236 | + self.canvas.unbind_all("<MouseWheel>") |
| 237 | + |
| 238 | + def _on_mousewheel(self, event) -> None: |
| 239 | + """ |
| 240 | + A callback for the mousewheel to scroll. |
| 241 | +
|
| 242 | + :param event: An event that Tkinter passes in. |
| 243 | + :return: None. |
| 244 | + """ |
| 245 | + if Platform.on_aqua(self): |
| 246 | + scroll = str(int(-1 * event.delta)) |
| 247 | + else: |
| 248 | + scroll = str(int(-1 * (event.delta / 120))) |
| 249 | + if self._shift_pressed: |
| 250 | + self.canvas.xview_scroll(scroll, tk.UNITS) |
| 251 | + else: |
| 252 | + self.canvas.yview_scroll(scroll, tk.UNITS) |
| 253 | + |
| 254 | + def _enable_children( |
| 255 | + self, parent: Union[tk.Widget, None] = None, enable: bool = True |
| 256 | + ) -> None: |
| 257 | + """ |
| 258 | + Enable or disable the children. |
| 259 | +
|
| 260 | + :param parent: A tk.Widget that is our parent. If None then default to |
| 261 | + self. |
| 262 | + :param enable: Whether to enable or disable the children. |
| 263 | + :return: None. |
| 264 | + """ |
| 265 | + if parent is None: |
| 266 | + parent = self.frame |
| 267 | + for child in parent.winfo_children(): |
| 268 | + if child.winfo_class() not in ("Frame", "LabelFrame"): |
| 269 | + try: |
| 270 | + child.state(["!disabled" if enable else "disabled"]) |
| 271 | + except AttributeError: |
| 272 | + child.configure(state=tk.NORMAL if enable else tk.DISABLED) |
| 273 | + else: |
| 274 | + self._enable_children(parent, enable) |
| 275 | + |
| 276 | + def apply_style(self, style_name: str) -> None: |
| 277 | + """ |
| 278 | + Apply a theme to this frame. |
| 279 | +
|
| 280 | + :param style_name: The name of the theme as a str, ex. "Warning" |
| 281 | + :return: None. |
| 282 | + """ |
| 283 | + if not isinstance(style_name, str): |
| 284 | + raise TypeError( |
| 285 | + f"style_name is not a str! " |
| 286 | + f"(type passed in: {repr(type(style_name))})" |
| 287 | + ) |
| 288 | + self.frame.configure(style=f"{style_name}.{self._style_root}") |
0 commit comments