Text inside ListItem is not aligned. #1732
-
|
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 1 reply
-
Thank you for your issue. Give us a little time to review it. PS. You might want to check the FAQ if you haven't done so already. This is an automated reply, generated by FAQtory |
Beta Was this translation helpful? Give feedback.
-
At first glance there looks to be quite a few different things going on in that code, and it doesn't look like I'd be able to test it out when I get to my desk as it doesn't look like it will run in isolation. Do you think you could isolate the issue in a small standalone example, ideally illustrating what you expected to see and why, and how the result differs? |
Beta Was this translation helpful? Give feedback.
-
@davep But how could I align text inside ListItem by CSS? |
Beta Was this translation helpful? Give feedback.
-
I suspect there's a bit of confusion here about what a from textual.app import App, ComposeResult
from textual.widgets import ListView, ListItem, Label, Footer
class ListViewExample(App):
CSS_PATH = "list_view.css"
def compose(self) -> ComposeResult:
yield ListView(
ListItem(Label("One")),
ListItem(Label("Two")),
ListItem(Label("Three")),
)
yield Footer()
if __name__ == "__main__":
app = ListViewExample()
app.run() Running the above code (with the CSS in place too) gives this: So, if we wanted to (for example) right-align that text, we'd want to ensure that each from textual.app import App, ComposeResult
from textual.widgets import ListView, ListItem, Label, Footer
class ListViewExample(App):
CSS = """
Screen {
align: center middle;
}
ListView {
width: 30;
height: auto;
margin: 2 2;
}
Label {
width: 100%;
text-align: right;
padding: 1 2;
}
"""
def compose(self) -> ComposeResult:
yield ListView(
ListItem(Label("One")),
ListItem(Label("Two")),
ListItem(Label("Three")),
)
yield Footer()
if __name__ == "__main__":
app = ListViewExample()
app.run() then you get this: The really handy part about this is you can combine different sorts of widgets within the from textual.app import App, ComposeResult
from textual.widgets import ListView, ListItem, Label, Footer
class ListViewExample(App):
CSS = """
Screen {
align: center middle;
}
ListView {
width: 30;
height: auto;
margin: 2 2;
}
ListItem {
layout: horizontal;
}
Label {
width: 1fr;
padding: 1 2;
}
Label.left {
text-align: left;
}
Label.middle {
text-align: center;
}
Label.right {
text-align: right;
}
"""
def compose(self) -> ComposeResult:
yield ListView(
ListItem(Label("One", classes="left"), Label("One", classes="middle"), Label( "One", classes="right")),
ListItem(Label("Two", classes="left"), Label("Two", classes="middle"), Label( "Two", classes="right")),
ListItem(Label("Three", classes="left"), Label("Three", classes="middle"), Label( "Three", classes="right")),
)
yield Footer()
if __name__ == "__main__":
app = ListViewExample()
app.run() You get this: |
Beta Was this translation helpful? Give feedback.
I suspect there's a bit of confusion here about what a
ListItem
is. AListItem
is intended to be a container for other widgets. So, if you look at the example code for aListView
you'll see that eachListItem
contains aLabel
:Running the above code (…