Sunday, September 28, 2025
HomePythonANN - The textual-cogs Bundle - Creating Reusable Dialogs for Textual

ANN – The textual-cogs Bundle – Creating Reusable Dialogs for Textual


Textual-cogs is a set of Textual dialogs that you should use in your Textual utility. You may see a fast demo of the dialogs beneath:

Dialogs included thus far:

  • Generic MessageDialog – reveals messages to the consumer
  • SaveFileDialog – provides the consumer a method to choose a location to save lots of a file
  • SingleChoiceDialog – provides the consumer a sequence of selections to select from
  • TextEntryDialog – ask the consumer a query and get their reply utilizing an Enter widget
  • and extra

You may try textual-cogs on GitHub.

You may set up textual-cog utilizing pip:

python -m pip set up textual-cog

You additionally want Textual to run these dialogs.

Right here is an instance of making a small utility that opens the MessageDialog instantly. You’ll usually open the dialog in response to a message or occasion that has occurred, reminiscent of when the appliance has an error or you should inform the consumer one thing.

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(worth: None | bool) -> None:
            self.exit()

        self.push_screen(
            MessageDialog(
                "What's your favourite language?",
                icon=icons.ICON_QUESTION,
                title="Warning",
            ),
            my_callback,
        )


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

Once you run this code, you’ll get one thing like the next:

MessageDialog from textual-cogs

 

Making a SaveFileDialog

 

The next code demonstrates find out 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()

Once you run this code, you will note the next:

SaveFileDialog from textual-cogs

Wrapping Up

The textual-cogs package deal is at present solely a set of reusable dialogs on your Textual utility. Nevertheless, this may also help velocity up your capacity so as to add code to your TUI purposes as a result of the dialogs are taken care of for you.

Test it out on GitHub or the Python Bundle Index in the present day.

Wish to Study Extra?

In the event you’d prefer to be taught extra about Textual, try my ebook: Creating TUI Functions with Textual and Python, which you will discover on the next web sites:

Creating TUI Applications with Textual and Python (paperback)

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments