Why are widgets automatically stretching horizontally/vertically in this case? #3550
-
I'm getting started using Textual for a simple TUI. In my case, I have a screen with an overall 2x2 grid layout and the right column has from textual.app import App, ComposeResult
from textual.containers import Container, Horizontal
from textual.widget import Widget
from textual.widgets import Static
class Bar(Widget):
DEFAULT_CSS = '''
Bar {
background: yellow 20%;
}
'''
def compose(self) -> ComposeResult:
with Horizontal():
yield Static('label1')
yield Static('label2')
class Foo(Widget):
def compose(self) -> ComposeResult:
yield Static('header label')
yield Bar()
class Test(App):
CSS = '''
Screen {
layout: grid;
grid-size: 2;
grid-columns: 1fr 2fr;
}
.box {
border: solid green;
height: 100%;
}
#right-pane {
row-span: 2;
}
'''
def compose(self) -> ComposeResult:
with Container(classes = 'box'):
yield Foo()
yield Foo()
yield Static('', classes = 'box', id = 'right-pane')
yield Static('', classes = 'box')
if __name__ == "__main__":
Test().run() And here's what the resulting window looks like: This gets me the overall grid effect that I want. However, the content that I have started to populate in the upper-left pane doesn't lay out how I would expect it to. The I feel like I'm missing something fundamental here. The documentation suggests that widgets will not stretch vertically unless I explicitly tell it to, but it seems like the |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
In your code above, the What I imagine is happening here is that they're in need of some horizontal size styling. If you tweak your class Bar(Widget):
DEFAULT_CSS = '''
Bar {
background: yellow 20%;
}
Static {
width: auto;
}
'''
def compose(self) -> ComposeResult:
with Horizontal():
yield Static('label1')
yield Static('label2') so that |
Beta Was this translation helpful? Give feedback.
-
The As Dave pointed out, it's your Statics with are sized to 100% width by default. If you have done any web dev, then Statics are like divs by default. But you can give them auto width to make them behave more like a span. |
Beta Was this translation helpful? Give feedback.
It's not intuitive, but the reason that there is no space for the tables is that they have
max-height: 100%
, which doesn't work well with auto height.We will have a better solution in the future, for now you can work around it by setting a different value for max-height, e.g.
max-height: 100vh