Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # /// script
- # requires-python = ">=3.13"
- # dependencies = [
- # "textual",
- # ]
- # ///
- """
- This is a minimal reproducible example (MRE) to demonstrate a scroll-to-end issue in Textual.
- The uv in-file config is used to specify dependencies and Python version, so no separate environment is needed.
- ## How to Run
- ### Using uv (recommended):
- uv run scroll_end_textual.py
- ### Using pip:
- pip install textual
- python scroll_end_textual.py
- """
- from textual.app import App
- from textual.containers import ScrollableContainer, Vertical
- from textual.widgets import Static
- class SimpleApp(App):
- MAIN = "main"
- CONTENT_WRAPPER = "wrapper"
- INSTRUCTIONS = "Press \[Enter] to add lines, 'c' to clear"
- def compose(self):
- yield ScrollableContainer(Vertical(id=self.MAIN), id=self.CONTENT_WRAPPER)
- async def add_to_screen(self, widget):
- # This is how widgets are added
- main = self.get_widget_by_id(self.MAIN)
- await main.mount(widget)
- await self.scroll_to_end()
- async def scroll_to_end(self):
- # This is the method used to scroll to the end
- container = self.get_widget_by_id(self.CONTENT_WRAPPER)
- container.scroll_end()
- # Maintained original styles and application method
- async def on_mount(self):
- self._style_content_wrapper()
- self._style_content_area()
- await self.add_to_screen(Static(self.INSTRUCTIONS))
- def _style_content_wrapper(self):
- container = self.get_widget_by_id(self.CONTENT_WRAPPER)
- container.styles.flex = 1
- container.styles.height = "1fr"
- container.styles.border = None
- container.styles.padding = (1, 1)
- container.styles.overflow_y = "auto"
- def _style_content_area(self):
- main = self.get_widget_by_id(self.MAIN)
- main.styles.border = None
- main.styles.padding = (0, 0, 0, 1)
- main.styles.min_height = "100%"
- main.styles.overflow_y = "auto"
- # Demonstration Related Methods
- async def add_lines(self, n):
- for line_count in range(n):
- await self.add_to_screen(Static(f"Line {line_count + 1}"))
- await self.scroll_to_end()
- async def clear_main(self):
- main = self.get_widget_by_id(self.MAIN)
- await main.remove_children()
- async def on_key(self, event):
- if event.key == "enter":
- await self.add_lines(20)
- elif event.key == "c":
- await self.clear_main()
- await self.add_to_screen(Static(self.INSTRUCTIONS))
- #
- # Module Run
- #
- if __name__ == "__main__":
- SimpleApp().run()
Advertisement
Add Comment
Please, Sign In to add comment