TextArea seems to crash under special circumstances #4561
-
Hi, to start with: I have found a workaround for the following problem - see below. So it might be helpful for others or just interesting to explain the reason for it. I can't do the latter. In my application I need to define my own All that works fine for Here a minimal example. In the real world the from textual.app import App
from textual.widgets import Input, TextArea
class Main(App):
def __init__(self, driver_class=None, css_path=None, watch_css=False):
self.buildWidgets()
super().__init__(driver_class, css_path, watch_css)
def buildWidgets(self):
self.MyWidget = Input() # works
# self.MyWidget = TextArea() # doesn' work
def compose(self):
yield self.MyWidget
if __name__ == "__main__":
app = Main()
app.run() Running this with Traceback (most recent call last):
File "/home/ulrich/PythonHobby/CLUI/Textual/.venv/lib/python3.10/site-packages/textual/message_pump.py", line 209, in app
return active_app.get()
LookupError: <ContextVar name='active_app' at 0x7f3a88919120>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/ulrich/PythonHobby/CLUI/Textual/./Test.py", line 20, in <module>
app = Main()
File "/home/ulrich/PythonHobby/CLUI/Textual/./Test.py", line 9, in __init__
self.buildWidgets()
File "/home/ulrich/PythonHobby/CLUI/Textual/./Test.py", line 14, in buildWidgets
self.MyWidget = TextArea()
File "/home/ulrich/PythonHobby/CLUI/Textual/.venv/lib/python3.10/site-packages/textual/widgets/_text_area.py", line 437, in __init__
self._set_document(text, language)
File "/home/ulrich/PythonHobby/CLUI/Textual/.venv/lib/python3.10/site-packages/textual/widgets/_text_area.py", line 853, in _set_document
self.move_cursor((0, 0))
File "/home/ulrich/PythonHobby/CLUI/Textual/.venv/lib/python3.10/site-packages/textual/widgets/_text_area.py", line 1586, in move_cursor
self.selection = Selection.cursor(location)
File "/home/ulrich/PythonHobby/CLUI/Textual/.venv/lib/python3.10/site-packages/textual/reactive.py", line 276, in __set__
self._check_watchers(obj, name, current_value)
File "/home/ulrich/PythonHobby/CLUI/Textual/.venv/lib/python3.10/site-packages/textual/reactive.py", line 301, in _check_watchers
invoke_watcher(obj, private_watch_function, old_value, value)
File "/home/ulrich/PythonHobby/CLUI/Textual/.venv/lib/python3.10/site-packages/textual/reactive.py", line 77, in invoke_watcher
watch_result = cast(WatchCallbackBothValuesType, watch_function)(
File "/home/ulrich/PythonHobby/CLUI/Textual/.venv/lib/python3.10/site-packages/textual/widgets/_text_area.py", line 583, in _watch_selection
self.app.cursor_position = self.cursor_screen_offset
File "/home/ulrich/PythonHobby/CLUI/Textual/.venv/lib/python3.10/site-packages/textual/message_pump.py", line 216, in app
raise NoActiveAppError()
textual._context.NoActiveAppError It seems clear that building the Found Workaround?The last line Best regards |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Building widgets before your application has had the chance to initialise itself isn't generally going to be a good idea. At the very least you'd want to be making the |
Beta Was this translation helpful? Give feedback.
Within
compose
would be more common. In most code you'll find that acompose
method simply yields the widgets into the DOM, and then they are queried back when they're needed. See this dialog in one of my own applications, for example.Note by the way in your example code that you're calling
buildWidgets
twice, which means you're going to be building one set and then never using them, instead using the subsequently-built set.You should find that the tutorial introduces these ideas, and the guide goes on to build upon them. In particular the guide to App basics, and especially it's introduction of widgets and how to compose them should contain some useful background.