added remove random button

This commit is contained in:
Will McGugan
2022-08-06 13:18:43 +01:00
parent d45d8773f5
commit cdf7085797
3 changed files with 19 additions and 2 deletions

View File

@@ -1,3 +1,5 @@
import random
from textual import layout
from textual.app import App, ComposeResult
from textual.widgets import Button, Static
@@ -38,6 +40,7 @@ class AddRemoveApp(App):
layout.Horizontal(
Button("Add", variant="success", id="add"),
Button("Remove", variant="error", id="remove"),
Button("Remove random", variant="warning", id="remove_random"),
id="buttons",
),
layout.Vertical(id="items"),
@@ -54,6 +57,11 @@ class AddRemoveApp(App):
if things:
things.last().remove()
elif event.button.id == "remove_random":
things = self.query("#items Thing")
if things:
random.choice(things).remove()
self.app.bell()