The bug
Initially reported on discord: https://discord.com/channels/1026214085173461072/1033754296224841768/1483472126802661407
I ran into an issue when I tried to add the first Input.
It seems Input cannot be used with app.MODES, or at least not in the compose method of the first screen of the app.DEFAULT_MODE (I did not set app.SCREENS). That kills the app with a traceback for this error:
ScreenStackError: No screens on stack
The crash happens because Inputs watchers end up trying to do self.app.clear_selection().
|
def _watch_selection(self, selection: Selection) -> None: |
|
self.app.clear_selection() |
That has this try/except:
|
def clear_selection(self) -> None: |
|
"""Clear text selection on the active screen.""" |
|
try: |
|
self.screen.clear_selection() |
|
except NoScreen: |
|
pass |
That only catches NoScreen, but with modes a ScreenStackError is raised because that first screen hasn't been mounted yet, so the stack is still empty.
For now, I'm working around the issue by adding a clear_selection method to my app that does:
def clear_selection(self):
try:
super().clear_selection()
except ScreenStackError:
pass
This workaround is based on several places in the code base that do except NoScreen, ScreenStackError, so it seemed like a reasonable fix.
On discord, David said the following:
https://discord.com/channels/1026214085173461072/1033754296224841768/1483494995523534999
Interesting. I can reproduce this using the modes01.py example from the Guide (and replacing the dashboard Placeholder by an Input). HOWEVER, if you do not use DEFAULT_MODE but use the code in the modes01.py example, you don't run into this problem. So do this:
# DEFAULT_MODE = "dashboard"
def on_mount(self) -> None:
self.switch_mode("dashboard")
(change the name of course) and skip using DEFAULT_MODE, at least for now.
Textual Diagnostics
Versions
| Name |
Value |
| Textual |
8.1.1 |
| Rich |
14.3.2 |
Python
| Name |
Value |
| Version |
3.13.7 |
| Implementation |
CPython |
| Compiler |
Clang 20.1.4 |
| Executable |
/home/jafloyd/g/github/copart-pe/ghe.git/.venv/bin/python3 |
Operating System
| Name |
Value |
| System |
Linux |
| Release |
6.8.0-90-generic |
| Version |
#91~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Thu Nov 20 15:20:45 UTC 2 |
Terminal
| Name |
Value |
| Terminal Application |
Konsole 220403 |
| TERM |
xterm-256color |
| COLORTERM |
truecolor |
| FORCE_COLOR |
Not set |
| NO_COLOR |
Not set |
Rich Console options
| Name |
Value |
| size |
width=236, height=55 |
| legacy_windows |
False |
| min_width |
1 |
| max_width |
236 |
| is_terminal |
True |
| encoding |
utf-8 |
| max_height |
55 |
| justify |
None |
| overflow |
None |
| no_wrap |
False |
| highlight |
None |
| markup |
None |
| height |
None |
Have you checked closed issues? (https://github.com/Textualize/textual/issues?q=is%3Aissue+is%3Aclosed)
Have you checked against the most recent version of Textual? (https://pypi.org/search/?q=textual)
The bug
Initially reported on discord: https://discord.com/channels/1026214085173461072/1033754296224841768/1483472126802661407
I ran into an issue when I tried to add the first
Input.It seems
Inputcannot be used withapp.MODES, or at least not in thecomposemethod of the first screen of theapp.DEFAULT_MODE(I did not setapp.SCREENS). That kills the app with a traceback for this error:The crash happens because
Inputs watchers end up trying to doself.app.clear_selection().textual/src/textual/widgets/_input.py
Lines 507 to 508 in 0f0849f
That has this try/except:
textual/src/textual/app.py
Lines 4555 to 4560 in 0f0849f
That only catches
NoScreen, but with modes aScreenStackErroris raised because that first screen hasn't been mounted yet, so the stack is still empty.For now, I'm working around the issue by adding a
clear_selectionmethod to my app that does:This workaround is based on several places in the code base that do
except NoScreen, ScreenStackError, so it seemed like a reasonable fix.On discord, David said the following:
https://discord.com/channels/1026214085173461072/1033754296224841768/1483494995523534999
Textual Diagnostics
Versions
Python
Operating System
Terminal
Rich Console options