diff --git a/changes/2916.bugfix.rst b/changes/2916.bugfix.rst new file mode 100644 index 0000000000..8e89d77924 --- /dev/null +++ b/changes/2916.bugfix.rst @@ -0,0 +1 @@ +Fixed a bug where styles were being applied before the widget's implementation was available. diff --git a/core/src/toga/style/pack.py b/core/src/toga/style/pack.py index 8743293968..4c08ee70f1 100644 --- a/core/src/toga/style/pack.py +++ b/core/src/toga/style/pack.py @@ -94,7 +94,7 @@ def _hidden(self) -> bool: return self.visibility == HIDDEN def apply(self, prop: str, value: object) -> None: - if self._applicator: + if self._applicator and self._applicator.widget._impl: if prop == "text_align": if value is None: if self.text_direction == RTL: diff --git a/core/src/toga/widgets/base.py b/core/src/toga/widgets/base.py index 8a545916d4..7688106ef9 100644 --- a/core/src/toga/widgets/base.py +++ b/core/src/toga/widgets/base.py @@ -33,6 +33,8 @@ def __init__( :param style: A style object. If no style is provided, a default style will be applied to the widget. """ + self._impl: Any = None + super().__init__( style=style if style else Pack(), applicator=TogaApplicator(self), @@ -41,7 +43,6 @@ def __init__( self._id = str(id if id else identifier(self)) self._window: Window | None = None self._app: App | None = None - self._impl: Any = None self.factory = get_platform_factory() diff --git a/core/tests/style/pack/utils.py b/core/tests/style/pack/utils.py index 1c46b41822..b38d2ac77b 100644 --- a/core/tests/style/pack/utils.py +++ b/core/tests/style/pack/utils.py @@ -7,6 +7,8 @@ class ExampleNode(Node): def __init__(self, name, style, size=None, children=None): + self._impl = None + super().__init__( style=style, children=children, applicator=TogaApplicator(self) )