How to create scrollable grid layout? #2992
-
Hello! I want to create a grid of static widgets with a size of 20x20 (although the size may change). I would like the grid to be scrollable (to make it readable), but I am unsure how to achieve this. What I have tried:
Do you have any ideas how to solve it? Here is an image of what I would like to achieve (assuming that consecutive numbers become visible after scrolling to the right or down): |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
As you've discovered, from textual.app import App, ComposeResult
from textual.containers import VerticalScroll, Horizontal
from textual.widgets import Static
class ScrollableGridApp(App[None]):
CSS = """
.row {
height: 5;
}
.row Static {
border: solid cornflowerblue;
width: 1fr;
height: 1fr;
}
"""
def compose(self) -> ComposeResult:
with VerticalScroll():
for y in range(20):
with Horizontal(classes="row"):
for x in range(20):
yield Static(f"{y} {x}")
if __name__ == "__main__":
ScrollableGridApp().run() giving: |
Beta Was this translation helpful? Give feedback.
-
@davep - it seems that what I was missing was adding the size to the horizontal container. What you did is exactly what I meant. Thank you for your help! |
Beta Was this translation helpful? Give feedback.
As you've discovered,
grid
is for fitting things within the display, essentially. I think if I was going to do what you're asking here, I'd start out with this sort of framework: