Skip to content
Open
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
4 changes: 4 additions & 0 deletions src/magicgui/widgets/bases/_container_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ def __init__(
self._insert_widget(index, widget)
self.native_parent_changed.connect(self.reset_choices)
self._initialized = True
# label widths are not unified during __init__ (because _unify_label_widths
# is a no-op until _initialized is True), so do it once now that all the
# widgets passed to the constructor have been inserted.
self._unify_label_widths()

def __len__(self) -> int:
"""Return the count of widgets."""
Expand Down
35 changes: 35 additions & 0 deletions tests/test_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,41 @@ def _label_width():
container.close()


def test_container_label_widths_unified_on_construction():
"""Constructor and append should produce the same label alignment.

Regression test for https://github.com/pyapp-kit/magicgui/issues/729
"""

def _label_widths(container):
return [
w._labeled_widget().label_width
for w in container
if w._labeled_widget() is not None
]

via_constructor = widgets.Container(
widgets=[
widgets.LineEdit(label="Name", value="Ada"),
widgets.SpinBox(label="Very long label", value=3),
]
)
via_append = widgets.Container()
via_append.append(widgets.LineEdit(label="Name", value="Ada"))
via_append.append(widgets.SpinBox(label="Very long label", value=3))

constructor_widths = _label_widths(via_constructor)
append_widths = _label_widths(via_append)

# all labels within a container should share the same (widest) width
assert len(set(constructor_widths)) == 1
# and both construction methods should agree
assert constructor_widths == append_widths

via_constructor.close()
via_append.close()


@pytest.mark.parametrize("scrollable", [False, True])
def test_labeled_widget_container(scrollable):
"""Test that _LabeledWidgets follow their children."""
Expand Down
Loading