diff --git a/changes/2638.bugfix.rst b/changes/2638.bugfix.rst new file mode 100644 index 0000000000..449610133f --- /dev/null +++ b/changes/2638.bugfix.rst @@ -0,0 +1 @@ +The type of SplitContainer's content was modified to be a list, rather than a tuple. diff --git a/core/src/toga/widgets/splitcontainer.py b/core/src/toga/widgets/splitcontainer.py index 04589bc737..5d76f2a24a 100644 --- a/core/src/toga/widgets/splitcontainer.py +++ b/core/src/toga/widgets/splitcontainer.py @@ -1,6 +1,7 @@ from __future__ import annotations import sys +from collections.abc import Sequence from typing import TYPE_CHECKING from toga.app import App @@ -27,7 +28,7 @@ def __init__( id: str | None = None, style: StyleT | None = None, direction: Direction = Direction.VERTICAL, - content: tuple[SplitContainerContentT, SplitContainerContentT] = (None, None), + content: Sequence[SplitContainerContentT] | None = None, ): """Create a new SplitContainer. @@ -42,15 +43,13 @@ def __init__( of the container. Defaults to both panels being empty. """ super().__init__(id=id, style=style) - self._content: tuple[SplitContainerContentT, SplitContainerContentT] = ( - None, - None, - ) + self._content: list[SplitContainerContentT] = [None, None] # Create a platform specific implementation of a SplitContainer self._impl = self.factory.SplitContainer(interface=self) - self.content = content + if content: + self.content = content self.direction = direction @property @@ -71,7 +70,7 @@ def focus(self) -> None: pass @property - def content(self) -> tuple[SplitContainerContentT, SplitContainerContentT]: + def content(self) -> list[SplitContainerContentT]: """The widgets displayed in the SplitContainer. This property accepts a sequence of exactly 2 elements, each of which can be @@ -89,9 +88,7 @@ def content(self) -> tuple[SplitContainerContentT, SplitContainerContentT]: return self._content @content.setter - def content( - self, content: tuple[SplitContainerContentT, SplitContainerContentT] - ) -> None: + def content(self, content: Sequence[SplitContainerContentT]) -> None: try: if len(content) != 2: raise TypeError() @@ -128,10 +125,10 @@ def content( widget.window = self.window self._impl.set_content( - tuple(w._impl if w is not None else None for w in _content), + list(w._impl if w is not None else None for w in _content), flex, ) - self._content = tuple(_content) + self._content = list(_content) self.refresh() @Widget.app.setter diff --git a/core/tests/widgets/test_splitcontainer.py b/core/tests/widgets/test_splitcontainer.py index 62a61a0572..4d27599661 100644 --- a/core/tests/widgets/test_splitcontainer.py +++ b/core/tests/widgets/test_splitcontainer.py @@ -39,7 +39,7 @@ def test_widget_created(): assert splitcontainer._impl.interface == splitcontainer assert_action_performed(splitcontainer, "create SplitContainer") - assert splitcontainer.content == (None, None) + assert splitcontainer.content == [None, None] assert splitcontainer.direction == toga.SplitContainer.VERTICAL @@ -52,14 +52,14 @@ def test_widget_created_with_values(content1, content2): assert splitcontainer._impl.interface == splitcontainer assert_action_performed(splitcontainer, "create SplitContainer") - assert splitcontainer.content == (content1, content2) + assert splitcontainer.content == [content1, content2] assert splitcontainer.direction == toga.SplitContainer.HORIZONTAL # The content has been assigned to the widget assert_action_performed_with( splitcontainer, "set content", - content=(content1._impl, content2._impl), + content=[content1._impl, content2._impl], flex=[1, 1], ) @@ -212,10 +212,10 @@ def test_set_content_widgets( assert_action_performed_with( splitcontainer, "set content", - content=( + content=[ content2._impl if include_left else None, content3._impl if include_right else None, - ), + ], flex=[1, 1], ) @@ -248,10 +248,10 @@ def test_set_content_flex( assert_action_performed_with( splitcontainer, "set content", - content=( + content=[ content2._impl if include_left else None, content3._impl if include_right else None, - ), + ], flex=[2, 3], ) @@ -284,10 +284,10 @@ def test_set_content_flex_mixed( assert_action_performed_with( splitcontainer, "set content", - content=( + content=[ content2._impl if include_left else None, content3._impl if include_right else None, - ), + ], flex=[1, 3], ) diff --git a/docs/reference/api/containers/splitcontainer.rst b/docs/reference/api/containers/splitcontainer.rst index 6d04683daf..cbaaa8e563 100644 --- a/docs/reference/api/containers/splitcontainer.rst +++ b/docs/reference/api/containers/splitcontainer.rst @@ -49,7 +49,7 @@ Usage left_container = toga.Box() right_container = toga.ScrollContainer() - split = toga.SplitContainer(content=(left_container, right_container)) + split = toga.SplitContainer(content=[left_container, right_container]) Content can be specified when creating the widget, or after creation by assigning the ``content`` attribute. The direction of the split can also be configured, either @@ -65,7 +65,7 @@ at time of creation, or by setting the ``direction`` attribute: left_container = toga.Box() right_container = toga.ScrollContainer() - split.content = (left_container, right_container) + split.content = [left_container, right_container] By default, the space of the SplitContainer will be evenly divided between the two panels. To specify an uneven split, you can provide a flex value when specifying @@ -80,7 +80,7 @@ and right panels. left_container = toga.Box() right_container = toga.ScrollContainer() - split.content = ((left_container, 3), (right_container, 2)) + split.content = [(left_container, 3), (right_container, 2)] This only specifies the initial split; the split can be modified by the user once it is displayed.