Widget disappears when removed and mounted again #1445
Answered
by
davep
davidbrochart
asked this question in
Q&A
-
In the following example, a widget mounts a button when from textual.app import App, ComposeResult
from textual.widget import Widget
from textual.widgets import Button, Footer
class MyWidget(Widget):
def __init__(self):
self.button = Button()
super().__init__()
def compose(self) -> ComposeResult:
yield self.button
class MyApp(App):
BINDINGS = [("s", "show_button", "Show Button")]
def __init__(self):
self.my_widget = None
super().__init__()
def compose(self) -> ComposeResult:
yield Footer()
async def action_show_button(self) -> None:
if self.my_widget is None:
self.my_widget = MyWidget()
else:
await self.my_widget.remove()
await self.mount(self.my_widget)
if __name__ == "__main__":
app = MyApp()
app.run() |
Beta Was this translation helpful? Give feedback.
Answered by
davep
Dec 27, 2022
Replies: 1 comment 7 replies
-
To the best of my knowledge, when you |
Beta Was this translation helpful? Give feedback.
7 replies
Answer selected by
davidbrochart
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To the best of my knowledge, when you
remove
a widget you're, in effect, destroying it (for want of a better term). Once youremove
a particular instance you can't mont it again. If you want to add a button again you'll want tomount
a fresh instance ofMyWidget
.