Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type hints and other minor fixes #9

Merged
merged 5 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/dist/
/.vscode/
__pycache__
2 changes: 1 addition & 1 deletion examples/examples_ScrollableAreaQt5.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion examples/examples_ScrollableAreaQt6.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions examples/examples_ScrollableFrameTk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})")
Expand Down
4 changes: 2 additions & 2 deletions examples/examples_ScrollablePanelWx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 2 additions & 1 deletion src/ScrollableContainers/_qt5.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 2 additions & 1 deletion src/ScrollableContainers/_qt6.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
33 changes: 17 additions & 16 deletions src/ScrollableContainers/_tk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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.

Expand All @@ -87,22 +88,22 @@ 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.

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,
Expand All @@ -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("<Expose>", 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("<Button-4>", self._on_mouse_scroll)
self.bind_all("<Button-5>", self._on_mouse_scroll)
self.bind_all("<MouseWheel>", 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("<Button-4>")
self.unbind_all("<Button-5>")
self.unbind_all("<MouseWheel>")

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
Expand Down
7 changes: 4 additions & 3 deletions src/ScrollableContainers/_wx.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down