mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
remove widgets
This commit is contained in:
63
sandbox/will/add_remove.py
Normal file
63
sandbox/will/add_remove.py
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
from textual import layout
|
||||||
|
from textual.app import App, ComposeResult
|
||||||
|
from textual.widgets import Button, Static
|
||||||
|
|
||||||
|
|
||||||
|
class Thing(Static):
|
||||||
|
def on_show(self) -> None:
|
||||||
|
self.scroll_visible()
|
||||||
|
|
||||||
|
|
||||||
|
class AddRemoveApp(App):
|
||||||
|
CSS = """
|
||||||
|
#buttons {
|
||||||
|
dock: top;
|
||||||
|
height: auto;
|
||||||
|
}
|
||||||
|
Button {
|
||||||
|
width: 1fr;
|
||||||
|
}
|
||||||
|
#items {
|
||||||
|
height: 100%;
|
||||||
|
|
||||||
|
}
|
||||||
|
Thing {
|
||||||
|
height: 5;
|
||||||
|
background: $panel;
|
||||||
|
border: wide $primary;
|
||||||
|
margin: 0 1;
|
||||||
|
content-align: center middle;
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
|
||||||
|
def on_mount(self) -> None:
|
||||||
|
self.count = 0
|
||||||
|
|
||||||
|
def compose(self) -> ComposeResult:
|
||||||
|
yield layout.Vertical(
|
||||||
|
layout.Horizontal(
|
||||||
|
Button("Add", variant="success", id="add"),
|
||||||
|
Button("Remove", variant="error", id="remove"),
|
||||||
|
id="buttons",
|
||||||
|
),
|
||||||
|
layout.Vertical(id="items"),
|
||||||
|
)
|
||||||
|
|
||||||
|
def handle_pressed(self, event: Button.Pressed) -> None:
|
||||||
|
if event.button.id == "add":
|
||||||
|
self.count += 1
|
||||||
|
self.query("#items").first().mount(
|
||||||
|
Thing(f"Thing {self.count}", id=f"thing{self.count}")
|
||||||
|
)
|
||||||
|
elif event.button.id == "remove":
|
||||||
|
things = self.query("#items Thing")
|
||||||
|
if things:
|
||||||
|
things.last().remove()
|
||||||
|
|
||||||
|
self.app.bell()
|
||||||
|
|
||||||
|
|
||||||
|
app = AddRemoveApp()
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
app.run()
|
||||||
Reference in New Issue
Block a user