Is it possible to create TabbedContent
widget in on_mount
?
#5709
-
I make a trick of from textual.app import App
from textual.widgets import TabbedContent,TabPane,Static
# patch TabbedContent
class TabbedContent(TabbedContent):
def __init__(self,*children, titles = None, initial = "", name = None, id = None, classes = None, disabled = False):
self.titles = [self.render_str(title) for title in titles] if titles else []
super().__init__(*self.titles, initial=initial, name=name, id=id, classes=classes, disabled=disabled)
self._tab_content = list(children)
class MyApp(App):
# use TabbedContent in compose
def compose(self):
with TabbedContent(titles=['Yes','No']):
yield Static('[green]Yes')
yield Static('[red]No')
# use TabbedContent in on_mount
""" def on_mount(self):
self.widgets = [
TabbedContent(
TabPane(
'Yes',
Static('[green]Yes')
),
TabPane(
'No',
Static('[red]No')
)
)
]
self.mount_all(self.widgets) """
if __name__ == '__main__':
app = MyApp()
app.run() |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
We found the following entry in the FAQ which you may find helpful: Feel free to close this issue if you found an answer in the FAQ. Otherwise, please give us a little time to review. This is an automated reply, generated by FAQtory |
Beta Was this translation helpful? Give feedback.
-
Replace on_mount with on_ready, Worked first try for me, |
Beta Was this translation helpful? Give feedback.
-
I get a solution which is compatible to all ways: from textual.app import App
from textual.widgets import TabbedContent,TabPane,Static
# 补丁 TabbedContent
from textual.widgets import TabbedContent,TabPane
from textual.content import ContentType,Content
from textual.widget import Widget
class TabbedContent(TabbedContent):
def __init__(self,*children:list[TabPane|Widget]|list[ContentType], initial = '', name = None, id = None, classes = None, disabled = False):
super().__init__(initial=initial, name=name, id=id, classes=classes, disabled=disabled)
self.titles = []
if all(isinstance(child,Widget) for child in children):
self._tab_content = list(children)
elif all((isinstance(child,(Content,str)) or not child) for child in children):
self.titles = [(self.render_str(title) if title else None) for title in children]
else:
raise TypeError('Don\'t mix the type of children')
class MyApp(App):
# 在 compose 中使用 TabbedContent
def compose(self):
with TabbedContent('one','two','[yellow]three',None,initial='tab-3'):
yield TabPane(
'Yes',
Static('[green]Yes')
)
with TabPane(
'[yellow]Yes2'
):
yield Static('[yellow]Yes2')
yield Static('[red]No')
yield Static('[red]No')
yield Static('[red]No')
# 在 on_mount 或者 on_ready 中使用 TabbedContent
def on_mount(self):
self.widgets = [
TabbedContent(
TabPane(
'Yes',
Static('[green]Yes')
),
# 不可混用组件类型与(ContentType|None)类型
# Content('[red]Yes'),
TabPane(
'No',
Static('[red]No')
),
Static('[yellow]Yes2'),
initial='tab-3'
)
]
self.mount_all(self.widgets)
if __name__ == '__main__':
app = MyApp()
app.run() |
Beta Was this translation helpful? Give feedback.
I get a solution which is compatible to all ways: