-
How to add TapPane to a TabbedContent like |
Beta Was this translation helpful? Give feedback.
Answered by
Ljzd-PRO
Apr 28, 2023
Replies: 1 comment
-
Add a class DynamicTabbedContent(TabbedContent):
def __init__(self, *titles: TextType):
super().__init__(*titles)
self.tabs: Optional[Tabs] = None
self.content_switcher: Optional[ContentSwitcher] = None
@classmethod
def _set_id(cls, content: TabPane, new_id: str) -> TabPane:
"""Set an id on the content, if not already present.
Args:
content: a TabPane.
new_id: New `is` attribute, if it is not already set.
Returns:
The same TabPane.
"""
if content.id is None:
content.id = new_id
return content
def append(self, content: TabPane):
"""Add TabPane"""
self.titles.append(content._title)
self._tab_content.append(content)
tab_pane_with_id = self._set_id(content, f"tab-{len(self.content_switcher.children) + 1}")
content_tab = ContentTab(tab_pane_with_id._title, tab_pane_with_id.id or "")
await self.content_switcher.mount(tab_pane_with_id)
self.content_switcher._initial = self.active
self.content_switcher.on_mount()
self.tabs.add_tab(content_tab)
def compose(self) -> ComposeResult:
"""Compose the tabbed content."""
# Wrap content in a `TabPane` if required.
pane_content = [
(
self._set_id(content, f"tab-{index}")
if isinstance(content, TabPane)
else TabPane(
title or self.render_str(f"Tab {index}"), content, id=f"tab-{index}"
)
)
for index, (title, content) in enumerate(
zip_longest(self.titles, self._tab_content), 1
)
]
# Get a tab for each pane
tabs = [
ContentTab(content._title, content.id or "") for content in pane_content
]
# Yield the tabs
self.tabs = Tabs(*tabs, active=self._initial or None)
yield self.tabs
# Yield the content switcher and panes
self.content_switcher = ContentSwitcher(initial=self._initial or None)
with self.content_switcher:
yield from pane_content |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
Ljzd-PRO
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add a
append
method and modifyTabs
object andContentSwitcher
object to addTabPane
and it works.