From f9889c41b10917acf43c37b3f26c5652882c4071 Mon Sep 17 00:00:00 2001 From: Vishal Pankaj Chandratreya <19171016+tfpf@users.noreply.github.com> Date: Thu, 15 Aug 2024 12:11:45 +0530 Subject: [PATCH] Type hints and other minor fixes (#9) * Added information about `args` and `kwargs` of constructors * Double backticks in comments referencing code * Type hints * Ignore `.vscode` --- .gitignore | 1 + examples/examples_ScrollableAreaQt5.py | 2 +- examples/examples_ScrollableAreaQt6.py | 2 +- examples/examples_ScrollableFrameTk.py | 4 ++-- examples/examples_ScrollablePanelWx.py | 4 ++-- src/ScrollableContainers/_qt5.py | 3 ++- src/ScrollableContainers/_qt6.py | 3 ++- src/ScrollableContainers/_tk.py | 33 +++++++++++++------------- src/ScrollableContainers/_wx.py | 7 +++--- 9 files changed, 32 insertions(+), 27 deletions(-) diff --git a/.gitignore b/.gitignore index 181430d..b83c549 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /dist/ +/.vscode/ __pycache__ diff --git a/examples/examples_ScrollableAreaQt5.py b/examples/examples_ScrollableAreaQt5.py index 506781a..4f30878 100755 --- a/examples/examples_ScrollableAreaQt5.py +++ b/examples/examples_ScrollableAreaQt5.py @@ -19,7 +19,7 @@ def grid_of_widgets(self, window): # Create a scrollable area. scrollable_area = ScrollableAreaQt5() - # Add widgets to the `area` attribute of the scrollable area, not to + # Add widgets to the ``area`` attribute of the scrollable area, not to # the scrollable area itself. dim = 10 grid_layout = QGridLayout(scrollable_area.area) diff --git a/examples/examples_ScrollableAreaQt6.py b/examples/examples_ScrollableAreaQt6.py index 733267d..93635b9 100755 --- a/examples/examples_ScrollableAreaQt6.py +++ b/examples/examples_ScrollableAreaQt6.py @@ -19,7 +19,7 @@ def grid_of_widgets(self, window): # Create a scrollable area. scrollable_area = ScrollableAreaQt6() - # Add widgets to the `area` attribute of the scrollable area, not to + # Add widgets to the ``area`` attribute of the scrollable area, not to # the scrollable area itself. dim = 10 grid_layout = QGridLayout(scrollable_area.area) diff --git a/examples/examples_ScrollableFrameTk.py b/examples/examples_ScrollableFrameTk.py index 88f94e3..8cc7ea0 100755 --- a/examples/examples_ScrollableFrameTk.py +++ b/examples/examples_ScrollableFrameTk.py @@ -18,8 +18,8 @@ def grid_of_widgets(self, window): # Create a scrollable frame. scrollable_frame = ScrollableFrameTk(window) - # Add widgets to the `frame` attribute of the scrollable frame, not to - # the scrollable frame itself. + # Add widgets to the ``frame`` attribute of the scrollable frame, not + # to the scrollable frame itself. dim = 10 for i, j in itertools.product(range(dim), repeat=2): label = ttk.Label(scrollable_frame.frame, text=f"Label\n({i}, {j})") diff --git a/examples/examples_ScrollablePanelWx.py b/examples/examples_ScrollablePanelWx.py index 23ec8c2..8ccd2b9 100755 --- a/examples/examples_ScrollablePanelWx.py +++ b/examples/examples_ScrollablePanelWx.py @@ -17,8 +17,8 @@ def grid_of_widgets(self, window): # Create a scrollable panel. scrollable_panel = ScrollablePanelWx(window) - # Add widgets to the `panel` attribute of the scrollable panel, not to - # the scrollable panel itself. + # Add widgets to the ``panel`` attribute of the scrollable panel, not + # to the scrollable panel itself. dim = 10 grid_sizer = wx.GridSizer(dim, dim, 20, 20) for i, j in itertools.product(range(dim), repeat=2): diff --git a/src/ScrollableContainers/_qt5.py b/src/ScrollableContainers/_qt5.py index 6fe38df..2cfe5ea 100644 --- a/src/ScrollableContainers/_qt5.py +++ b/src/ScrollableContainers/_qt5.py @@ -7,7 +7,8 @@ class ScrollableAreaQt5(QScrollArea): """ Container with horizontal and vertical scrolling capabilities. Widgets must - be added to its `area` attribute. + be added to its ``area`` attribute. Constructor arguments are passed to the + parent constructor. """ def __init__(self, *args, **kwargs): diff --git a/src/ScrollableContainers/_qt6.py b/src/ScrollableContainers/_qt6.py index f862b78..fc599ce 100644 --- a/src/ScrollableContainers/_qt6.py +++ b/src/ScrollableContainers/_qt6.py @@ -7,7 +7,8 @@ class ScrollableAreaQt6(QScrollArea): """ Container with horizontal and vertical scrolling capabilities. Widgets must - be added to its `area` attribute. + be added to its ``area`` attribute. Constructor arguments are passed to the + parent constructor. """ def __init__(self, *args, **kwargs): diff --git a/src/ScrollableContainers/_tk.py b/src/ScrollableContainers/_tk.py index ac38597..749fb0b 100644 --- a/src/ScrollableContainers/_tk.py +++ b/src/ScrollableContainers/_tk.py @@ -10,7 +10,8 @@ class ScrollableFrameTk(ttk.Frame): """ Container with horizontal and vertical scrolling capabilities. Widgets must - be added to its `frame` attribute. + be added to its ``frame`` attribute. Constructor arguments are passed to + the parent constructor. """ def __init__(self, *args, **kwargs): @@ -45,16 +46,16 @@ def __init__(self, *args, **kwargs): self._canvas.xview_moveto(0.0) self._canvas.yview_moveto(0.0) - def _xview(self, *args, width=None): + def _xview(self, *args, width: int | None = None): """ Called when a horizontal scroll is requested. Called by other callbacks - (`_on_canvas_configure` and `_on_frame_configure`) whenever it is + (``_on_canvas_configure`` and ``_on_frame_configure``) whenever it is necessary to horizontally realign the contents of the canvas. Scroll the view only if the contents are not completely visible. Otherwise, move the scrollbar to such a position that they are horizontally centred. - :param args: Tuple which can be passed to `tkinter.Canvas.xview`. + :param args: Passed to ``tkinter.Canvas.xview``. :param width: Width of the canvas. """ if self._canvas.xview() != (0.0, 1.0): @@ -73,12 +74,12 @@ def _yview(self, *args): Called when a vertical scroll is requested. Scroll the view only if the contents are not completely visible. - :param args: Tuple which can be passed to `tkinter.Canvas.yview`. + :param args: Passed to ``tkinter.Canvas.yview``. """ if self._canvas.yview() != (0.0, 1.0): self._canvas.yview(*args) - def _on_canvas_configure(self, event): + def _on_canvas_configure(self, event: tk.Event): """ Called when the canvas is resized. Update the scrollable region. @@ -87,7 +88,7 @@ def _on_canvas_configure(self, event): self._canvas.configure(scrollregion=self._canvas.bbox(tk.ALL)) self._xview(tk.SCROLL, 0, tk.UNITS, width=event.width) - def _on_frame_configure(self, _=None): + def _on_frame_configure(self, _event: tk.Event | None = None): """ Called when the frame is resized or the canvas is scrolled. Update the scrollable region. @@ -95,14 +96,14 @@ def _on_frame_configure(self, _=None): This method is necessary to handle updates which may occur after the GUI loop has started. - :param _: Configure event. + :param _event: Configure event. """ self._canvas.configure(scrollregion=self._canvas.bbox(tk.ALL)) self._xview(tk.SCROLL, 0, tk.UNITS) - def _on_frame_expose(self, _=None): + def _on_frame_expose(self, _event: tk.Event | None = None): """ - Called when the frame becomes visible. Call `_on_frame_configure` and + Called when the frame becomes visible. Call ``_on_frame_configure`` and then disable this callback. This method is necessary because if a scrollable frame is put into, @@ -112,34 +113,34 @@ def _on_frame_expose(self, _=None): because its frame configure events work differently.) Hence, I try to centre the contents again upon an expose event. - :param _: Expose event. + :param _event: Expose event. """ self._on_frame_configure() self.frame.unbind("", self._on_frame_expose_id) - def _on_canvas_enter(self, _=None): + def _on_canvas_enter(self, _event: tk.Event | None = None): """ Called when the mouse pointer enters the canvas. Set up vertical scrolling with the mouse wheel. - :param _: Enter event. + :param _event: Enter event. """ self.bind_all("", self._on_mouse_scroll) self.bind_all("", self._on_mouse_scroll) self.bind_all("", self._on_mouse_scroll) - def _on_canvas_leave(self, _=None): + def _on_canvas_leave(self, _event: tk.Event | None = None): """ Called when the mouse pointer leaves the canvas. Unset vertical scrolling with the mouse wheel. - :param _: Leave event. + :param _event: Leave event. """ self.unbind_all("") self.unbind_all("") self.unbind_all("") - def _on_mouse_scroll(self, event): + def _on_mouse_scroll(self, event: tk.Event): """ Called when the mouse wheel is scrolled or a two-finger swipe gesture is performed on the touchpad. Ask to scroll the view horizontally if diff --git a/src/ScrollableContainers/_wx.py b/src/ScrollableContainers/_wx.py index c1998cd..b3e1b2c 100644 --- a/src/ScrollableContainers/_wx.py +++ b/src/ScrollableContainers/_wx.py @@ -1,13 +1,14 @@ __all__ = ["ScrollablePanelWx"] import wx -from wx.lib import scrolledpanel +from wx.lib.scrolledpanel import ScrolledPanel -class ScrollablePanelWx(scrolledpanel.ScrolledPanel): +class ScrollablePanelWx(ScrolledPanel): """ Container with horizontal and vertical scrolling capabilities. Widgets must - be added to its `panel` attribute. + be added to its ``panel`` attribute. Constructor arguments are passed to + the parent constructor. """ def __init__(self, *args, **kwargs):