Skip to content

Commit 3d9a97e

Browse files
authored
Protect accessible attributes from modification (#16)
* Made `ScrollableFrameTk.frame` read-only * Made `ScrollablePanelWx.panel` read-only * Made `ScrollableAreaQt5.area` read-only * Made `ScrollableAreaQt6.area` read-only * Version bump
1 parent 33cf914 commit 3d9a97e

File tree

5 files changed

+29
-13
lines changed

5 files changed

+29
-13
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "ScrollableContainers"
7-
version = "2.1.0"
7+
version = "2.1.1"
88
authors = [
99
{ name = "Vishal Pankaj Chandratreya" },
1010
]

src/ScrollableContainers/_qt5.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ def __init__(self, *args, **kwargs):
1818
container = QWidget()
1919
self.setWidget(container)
2020
vbox = QVBoxLayout(container)
21-
self.area = QWidget()
22-
vbox.addWidget(self.area, alignment=Qt.AlignHCenter)
21+
self._area = QWidget()
22+
vbox.addWidget(self._area, alignment=Qt.AlignHCenter)
2323
vbox.addStretch()
2424
self.setWidgetResizable(True)
25+
26+
@property
27+
def area(self):
28+
return self._area

src/ScrollableContainers/_qt6.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ def __init__(self, *args, **kwargs):
1818
container = QWidget()
1919
self.setWidget(container)
2020
vbox = QVBoxLayout(container)
21-
self.area = QWidget()
22-
vbox.addWidget(self.area, alignment=Qt.AlignmentFlag.AlignHCenter)
21+
self._area = QWidget()
22+
vbox.addWidget(self._area, alignment=Qt.AlignmentFlag.AlignHCenter)
2323
vbox.addStretch()
2424
self.setWidgetResizable(True)
25+
26+
@property
27+
def area(self):
28+
return self._area

src/ScrollableContainers/_tk.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,21 @@ def __init__(self, *args, **kwargs):
4242
self.grid_rowconfigure(0, weight=1)
4343
self.grid_columnconfigure(0, weight=1)
4444

45-
self.frame = ttk.Frame(self._canvas)
46-
self._window = self._canvas.create_window((0, 0), window=self.frame, anchor=tk.NW)
47-
self.frame.bind("<Configure>", self._on_frame_configure)
48-
self._on_frame_expose_id = self.frame.bind("<Expose>", self._on_frame_expose)
45+
self._frame = ttk.Frame(self._canvas)
46+
self._window = self._canvas.create_window((0, 0), window=self._frame, anchor=tk.NW)
47+
self._frame.bind("<Configure>", self._on_frame_configure)
48+
self._on_frame_expose_id = self._frame.bind("<Expose>", self._on_frame_expose)
4949

5050
# Initially, the vertical scrollbar is a hair below its topmost
5151
# position. Move it to said position. No harm in doing the equivalent
5252
# for the horizontal scrollbar.
5353
self._canvas.xview_moveto(0.0)
5454
self._canvas.yview_moveto(0.0)
5555

56+
@property
57+
def frame(self):
58+
return self._frame
59+
5660
def _show_scrollbars(self):
5761
"""
5862
Move the horizontal and vertical scrollbars above the scrollable
@@ -120,7 +124,7 @@ def _xview(self, *args, width: int | None = None):
120124
# function with a negative argument. I don't know if this hack is
121125
# supported (because the Tcl/Tk manual pages say that it must be a
122126
# fraction between 0 and 1), but it works!
123-
self._canvas.xview_moveto((1 - width / self.frame.winfo_width()) / 2)
127+
self._canvas.xview_moveto((1 - width / self._frame.winfo_width()) / 2)
124128

125129
def _yview(self, *args):
126130
"""
@@ -169,7 +173,7 @@ def _on_frame_expose(self, _event: tk.Event | None = None):
169173
:param _event: Expose event.
170174
"""
171175
self._on_frame_configure()
172-
self.frame.unbind("<Expose>", self._on_frame_expose_id)
176+
self._frame.unbind("<Expose>", self._on_frame_expose_id)
173177

174178
def _on_canvas_enter(self, _event: tk.Event | None = None):
175179
"""

src/ScrollableContainers/_wx.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ def __init__(self, *args, **kwargs):
1717
# According to the documentation, a sizer is required to calculate the
1818
# minimum virtual size of the panel.
1919
vbox = wx.BoxSizer(wx.VERTICAL)
20-
self.panel = wx.Panel(self)
21-
vbox.Add(self.panel, flag=wx.ALIGN_CENTRE)
20+
self._panel = wx.Panel(self)
21+
vbox.Add(self._panel, flag=wx.ALIGN_CENTRE)
2222
self.SetSizer(vbox)
23+
24+
@property
25+
def panel(self):
26+
return self._panel

0 commit comments

Comments
 (0)