DataTable cell text style setting! #4043
Answered
by
TomJGooding
Yurii-huang
asked this question in
Q&A
-
from textual.events import Key
from textual.widgets import DataTable
from rich.style import Style
class Options(DataTable):
ROWS = [
("Options", "Stauts"),
("Configure the network", "no stting"),
("Set up users and password", "no stting"),
("Clock from NTP server", "no stting"),
("Disks partition", "no stting"),
("Set up bootloader", "no stting"),
("Choose operation after installed", "no stting"),
("Start the installation", ">>>>>>")
]
ROWS_TEXT_STYLE = [
(Style(), Style()),
(Style(), Style(color="red", bold=True)),
(Style(), Style(color="red", bold=True)),
(Style(), Style(color="red", bold=True)),
(Style(), Style(color="red", bold=True)),
(Style(), Style(color="red", bold=True)),
(Style(), Style(color="red", bold=True)),
(Style(), Style(color="green", bold=True))
]
def __init__(self):
super().__init__()
self.add_columns(*self.ROWS[0])
self.add_rows(self.ROWS[1:])
self.show_header = True
self.cursor_type = "row"
def _get_row_renderables(self, row_index: int):
ret = super()._get_row_renderables(row_index)
if self.show_header: # has row -1 offset
row_index += 1
styles = self.ROWS_TEXT_STYLE[row_index]
for col in range(0, len(ret.cells)):
rich_text = ret.cells[col]
rich_text.style = styles[col]
ret.cells[col] = rich_text
return ret |
Beta Was this translation helpful? Give feedback.
Answered by
TomJGooding
Jan 18, 2024
Replies: 2 comments 7 replies
-
Try changing the ROWS_TEXT_STYLE to dynamically change the Text Style |
Beta Was this translation helpful? Give feedback.
0 replies
-
There's a section about Styling and justifying cells in the DataTable docs. You really don't need to be messing with internal functions at all, just use a |
Beta Was this translation helpful? Give feedback.
7 replies
Answer selected by
Yurii-huang
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There's a section about Styling and justifying cells in the DataTable docs. You really don't need to be messing with internal functions at all, just use a
Text
object.