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

Setting self.dark in on_load doesn't work: No Screens on stack #1369

Closed
willmcgugan opened this issue Dec 15, 2022 · 3 comments
Closed

Setting self.dark in on_load doesn't work: No Screens on stack #1369

willmcgugan opened this issue Dec 15, 2022 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@willmcgugan
Copy link
Collaborator

Suspect we could make this work.

@davep davep self-assigned this Dec 21, 2022
@davep davep added the bug Something isn't working label Dec 21, 2022
@davep
Copy link
Contributor

davep commented Dec 21, 2022

Minimal code to demonstrate the issue:

from textual.app        import App, ComposeResult
from textual.containers import Container
from textual.widgets    import Header, Footer

class EarlyDark( App[ None ] ):

    CSS = """
    Container {
        background: $panel;
        border: solid $panel-lighten-2;
    }
    """

    BINDINGS = [
        ( "d", "toggle", "Toggle Light/Dark" )
    ]

    def compose( self ) -> ComposeResult:
        yield Header()
        yield Container()
        yield Footer()

    def action_toggle( self ) -> None:
        self.dark = not self.dark

    def on_load( self ) -> None:
        self.dark = False

if __name__ == "__main__":
    EarlyDark().run()

The result of running this is the following error on startup:

╭──────────────────────────────────────────────────────────── Traceback (most recent call last) ─────────────────────────────────────────────────────────────╮
│ /Users/davep/develop/python/textual-sandbox/early_dark.py:27 in on_load                                                                                    │
│                                                                                                                                                            │
│   24 │   │   self.dark = not self.dark                                                                                                                     │
│   25 │                                                                                                                                                     │
│   26 │   def on_load( self ) -> None:                                                                                                                      │
│ ❱ 27 │   │   self.dark = False                                                                                                                             │
│   28                                                                                                                                                       │
│   29 if __name__ == "__main__":                                                                                                                            │
│   30 │   EarlyDark().run()                                                                                                                                 │
│                                                                                                                                                            │
│ ╭─────────────────────────── locals ───────────────────────────╮                                                                                           │
│ │ self = EarlyDark(title='EarlyDark', classes={'-light-mode'}) │                                                                                           │
│ ╰──────────────────────────────────────────────────────────────╯                                                                                           │
│                                                                                                                                                            │
│ /Users/davep/develop/python/textual-sandbox/.venv/lib/python3.10/site-packages/textual/app.py:463 in watch_dark                                            │
│                                                                                                                                                            │
│    460 │   │   """Watches the dark bool."""                                                                                                                │
│    461 │   │   self.set_class(dark, "-dark-mode")                                                                                                          │
│    462 │   │   self.set_class(not dark, "-light-mode")                                                                                                     │
│ ❱  463 │   │   self.refresh_css()                                                                                                                          │
│    464 │                                                                                                                                                   │
│    465 │   def get_driver_class(self) -> Type[Driver]:                                                                                                     │
│    466 │   │   """Get a driver class for this platform.                                                                                                    │
│                                                                                                                                                            │
│ ╭─────────────────────────── locals ───────────────────────────╮                                                                                           │
│ │ dark = False                                                 │                                                                                           │
│ │ self = EarlyDark(title='EarlyDark', classes={'-light-mode'}) │                                                                                           │
│ ╰──────────────────────────────────────────────────────────────╯                                                                                           │
│                                                                                                                                                            │
│ /Users/davep/develop/python/textual-sandbox/.venv/lib/python3.10/site-packages/textual/app.py:1627 in refresh_css                                          │
│                                                                                                                                                            │
│   1624 │   │   stylesheet.set_variables(self.get_css_variables())                                                                                          │
│   1625 │   │   stylesheet.reparse()                                                                                                                        │
│   1626 │   │   stylesheet.update(self.app, animate=animate)                                                                                                │
│ ❱ 1627 │   │   self.screen._refresh_layout(self.size, full=True)                                                                                           │
│   1628 │                                                                                                                                                   │
│   1629 │   def _display(self, screen: Screen, renderable: RenderableType | None) -> None:                                                                  │
│   1630 │   │   """Display a renderable within a sync.                                                                                                      │
│                                                                                                                                                            │
│ ╭────────────────────────────────────────────────── locals ──────────────────────────────────────────────────╮                                             │
│ │    animate = True                                                                                          │                                             │
│ │       self = EarlyDark(title='EarlyDark', classes={'-light-mode'})                                         │                                             │
│ │ stylesheet = <Stylesheet                                                                                   │                                             │
│ │              │   [                                                                                         │                                             │
│ │              │   │   '/Users/davep/develop/python/textual-sandbox/early_dark.py:EarlyDark',                │                                             │
│ │              │   │   '/Users/davep/develop/python/textual-sandbox/.venv/lib/python3.10/site-packages/t'+17 │                                             │
│ │              │   ]                                                                                         │                                             │
│ │              >                                                                                             │                                             │
│ ╰────────────────────────────────────────────────────────────────────────────────────────────────────────────╯                                             │
│                                                                                                                                                            │
│ /Users/davep/develop/python/textual-sandbox/.venv/lib/python3.10/site-packages/textual/app.py:513 in screen                                                │
│                                                                                                                                                            │
│    510 │   │   try:                                                                                                                                        │
│    511 │   │   │   return self._screen_stack[-1]                                                                                                           │
│    512 │   │   except IndexError:                                                                                                                          │
│ ❱  513 │   │   │   raise ScreenStackError("No screens on stack") from None                                                                                 │
│    514 │                                                                                                                                                   │
│    515 │   @property                                                                                                                                       │
│    516 │   def size(self) -> Size:                                                                                                                         │
│                                                                                                                                                            │
│ ╭─────────────────────────── locals ───────────────────────────╮                                                                                           │
│ │ self = EarlyDark(title='EarlyDark', classes={'-light-mode'}) │                                                                                           │
│ ╰──────────────────────────────────────────────────────────────╯                                                                                           │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
ScreenStackError: No screens on stack

davep added a commit to davep/textual that referenced this issue Dec 21, 2022
This commit addresses Textualize#1369 by swallowing any `ScreenStackError` when
calling `refresh_css` after setting the -{dark,light}-mode class on the app.
The idea here being that if there is no screen, that's fine, the style will
be taken up once there is one.
@davep
Copy link
Contributor

davep commented Dec 21, 2022

Fixed by #1421.

@davep davep closed this as completed Dec 21, 2022
@github-actions
Copy link

Don't forget to star the repository!

Follow @textualizeio for Textual updates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants