Skip to content

Simple dialogs for the Textual TUI Python package

License

Notifications You must be signed in to change notification settings

erikw/textual-cogs

 
 

Repository files navigation

textual-cogs

A collection of Textual dialogs.

screenshot

Dialogs included so far:

  • Generic MessageDialog - shows messages to the user
  • SaveFileDialog - gives the user a way to select a location to save a file
  • SingleChoiceDialog - gives the user a series of choices to pick from
  • TextEntryDialog - ask the user a question and get their answer using an Input widget

Installation

You can install textual-cog using pip:

python -m pip install textual-cog

You also need Textual to run these dialogs.

Example Usage

Here is an example of creating a small application that opens the MessageDialog immediately. You would normally open the dialog in response to a message or event that has occurred, such as when the application has an error or you need to tell the user something.

from textual.app import App
from textual.app import App, ComposeResult

from textual_cogs.dialogs import MessageDialog
from textual_cogs import icons


class DialogApp(App):
    def on_mount(self) -> ComposeResult:
        def my_callback(value: None | bool) -> None:
            self.exit()

        self.push_screen(
            MessageDialog(
                "What is your favorite language?",
                icon=icons.ICON_QUESTION,
                title="Warning",
            ),
            my_callback,
        )


if __name__ == "__main__":
    app = DialogApp()
    app.run()

When you run this code, you will get something like the following:

screenshot

Creating a TextEntryDialog

Here is how you would create a TextEntryDialog:

from textual.app import App
from textual.app import App, ComposeResult

from textual_cogs.dialogs import TextEntryDialog


class DialogApp(App):
    def on_mount(self) -> ComposeResult:
        def my_callback(value: str | bool) -> None:
            self.exit()

        self.push_screen(
            TextEntryDialog("What is your name?", "Information"), my_callback
        )


if __name__ == "__main__":
    app = DialogApp()
    app.run()

When you run this code, you will see the following:

screenshot

Creating a SaveFileDialog

The following code demonstrates how to create a SaveFileDialog:

from textual.app import App
from textual.app import App, ComposeResult

from textual_cogs.dialogs import SaveFileDialog


class DialogApp(App):
    def on_mount(self) -> ComposeResult:        
        self.push_screen(SaveFileDialog())

if __name__ == "__main__":
    app = DialogApp()
    app.run()

When you run this code, you will see the following:

screenshot

About

Simple dialogs for the Textual TUI Python package

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 100.0%