With textual I'd like to build a simple program which presents me with different options I can choose from using OptionList
, but one by one, e.g.
First "screen":
what do you want to buy (Car/Bike)?
+---------+
| Car |
| > Bike |
+---------+
bike
And after I pressed/clicked on "Bike" I'd like to see the second 'screen' (with potentially different widgets):
electric (yes/no)?
+---------+
| Yes |
| > No |
+---------+
No
The following code shows me the first list of options but I have no idea how to proceed:
from textual.app import App, ComposeResult
from textual.widgets import Footer, Header, OptionList, Static
from textual import events, on
class SelectType(Static):
def compose(self) -> ComposeResult:
yield OptionList(
"Car",
"Bike",
)
@on(OptionList.OptionSelected)
def selected(self, *args):
return None # What to do here?
class MainProgram(App[None]):
def compose(self) -> ComposeResult:
yield Header()
yield Footer()
yield SelectType()
MainProgram().run()
What to do now? I crawled the tutorial, guides, examples but it looks like they all show me how to build one set of widgets but I didn't find a way to make a transition between one input screen and another one..
Depending on the scope and purpose of the application you're wanting to build, there's a few different approaches you could take here. Some options are:
TabbedContent
would be what you're looking for.ContentSwitcher
.compose
and then using a combination ofmount
andremove
.As suggested by Will in his answer, one very likely approach would be to use a
Screen
or two. With a little bit of thought you could probably turn it into quite a flexible and comprehensive application for asking questions.What follows is a very simplistic illustration of some of the approaches you could take. In it you'll find I've only put together a "bike" screen (with some placeholder questions), and only put in a placeholder screen for a car. Hopefully though it will illustrate some of the key ideas.
What's important here is that it uses
ModalScreen
and the screen callback facility to query the user and then get the data back to the main entry point.There are, of course, a lot of details "left for the reader"; do feel free to ask more about this if anything isn't clear in the example.