Prompt_toolkit ships with a high level API for displaying dialogs, similar to the Whiptail program, but in pure Python.
Use the ~prompt_toolkit.shortcuts.message_dialog
function to display a simple message box. For instance:
from prompt_toolkit.shortcuts import message_dialog
message_dialog(
title='Example dialog window',
text='Do you want to continue?\nPress ENTER to quit.')
The ~prompt_toolkit.shortcuts.input_dialog
function can display an input box. It will return the user input as a string.
from prompt_toolkit.shortcuts import input_dialog
text = input_dialog(
title='Input dialog example',
text='Please type your name:')
The password=True
option can be passed to the ~prompt_toolkit.shortcuts.input_dialog
function to turn this into a password input box.
The ~prompt_toolkit.shortcuts.yes_no_dialog
function displays a yes/no confirmation dialog. It will return a boolean according to the selection.
from prompt_toolkit.shortcuts import yes_no_dialog
result = yes_no_dialog(
title='Yes/No dialog example',
text='Do you want to confirm?')
The ~prompt_toolkit.shortcuts.button_dialog
function displays a dialog with choices offered as buttons. Buttons are indicated as a list of tuples, each providing the label (first) and return value if clicked (second).
from prompt_toolkit.shortcuts import button_dialog
result = button_dialog(
title='Button dialog example',
text='Do you want to confirm?',
buttons=[
('Yes', True),
('No', False),
('Maybe...', None)
],
)
A custom ~prompt_toolkit.styles.Style
instance can be passed to all dialogs to override the default style. Also, text can be styled by passing an ~prompt_toolkit.formatted_text.HTML
object.
from prompt_toolkit.formatted_text import HTML
from prompt_toolkit.shortcuts import message_dialog
from prompt_toolkit.styles import Style
example_style = Style.from_dict({
'dialog': 'bg:#88ff88',
'dialog frame-label': 'bg:#ffffff #000000',
'dialog.body': 'bg:#000000 #00ff00',
'dialog shadow': 'bg:#00aa00',
})
message_dialog(
title=HTML('<style bg="blue" fg="white">Styled</style> '
'<style fg="ansired">dialog</style> window'),
text='Do you want to continue?\nPress ENTER to quit.',
style=example_style)