Skip to content

Commit debbf4f

Browse files
committed
Add button_dialog, including docs/examples
1 parent d3c59f3 commit debbf4f

File tree

5 files changed

+70
-1
lines changed

5 files changed

+70
-1
lines changed

docs/images/dialogs/button.png

16.4 KB
Loading

docs/pages/dialogs.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,30 @@ selection.
6464
.. image:: ../images/dialogs/confirm.png
6565

6666

67+
Button dialog
68+
--------------------------
69+
70+
The :func:`~prompt_toolkit.shortcuts.dialogs.button_dialog` function displays a
71+
dialog with choices offered as buttons. Buttons are indicated as a list of
72+
tuples, each providing the label (first) and return value if clicked (second).
73+
74+
.. code:: python
75+
76+
from prompt_toolkit.shortcuts.dialogs import button_dialog
77+
78+
result = button_dialog(
79+
title='Button dialog example',
80+
text='Do you want to confirm?',
81+
buttons=[
82+
('Yes', True),
83+
('No', False),
84+
('Maybe...', None)
85+
],
86+
)
87+
88+
.. image:: ../images/dialogs/button.png
89+
90+
6791
Styling of dialogs
6892
------------------
6993

examples/dialogs/button_dialog.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env python
2+
"""
3+
Example of button dialog window.
4+
"""
5+
from __future__ import unicode_literals
6+
from prompt_toolkit.shortcuts.dialogs import button_dialog
7+
8+
9+
def main():
10+
result = button_dialog(
11+
title='Button dialog example',
12+
text='Are you sure?',
13+
buttons=[
14+
('Yes', True),
15+
('No', False),
16+
('Maybe...', None),
17+
],
18+
)
19+
20+
print('Result = {}'.format(result))
21+
22+
23+
if __name__ == '__main__':
24+
main()

prompt_toolkit/shortcuts/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from __future__ import unicode_literals
2-
from .dialogs import yes_no_dialog, input_dialog, message_dialog, radiolist_dialog, progress_dialog
2+
from .dialogs import yes_no_dialog, button_dialog, input_dialog, message_dialog, radiolist_dialog, progress_dialog
33
from .prompt import Prompt, prompt, confirm, create_confirm_prompt, CompleteStyle
44
from .utils import print_formatted_text, clear, set_title, clear_title
55

@@ -11,6 +11,7 @@
1111
'progress_dialog',
1212
'radiolist_dialog',
1313
'yes_no_dialog',
14+
'button_dialog',
1415

1516
# Prompts.
1617
'Prompt',

prompt_toolkit/shortcuts/dialogs.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from __future__ import unicode_literals
2+
import functools
23
from prompt_toolkit.application import Application
34
from prompt_toolkit.application.current import get_app
45
from prompt_toolkit.eventloop import run_in_executor
@@ -12,6 +13,7 @@
1213

1314
__all__ = [
1415
'yes_no_dialog',
16+
'button_dialog',
1517
'input_dialog',
1618
'message_dialog',
1719
'radiolist_dialog',
@@ -42,6 +44,24 @@ def no_handler():
4244
return _run_dialog(dialog, style, async_=async_)
4345

4446

47+
def button_dialog(title='', text='', buttons=[], style=None,
48+
async_=False):
49+
"""
50+
Display a dialog with button choices (given as a list of tuples).
51+
Return the value associated with button.
52+
"""
53+
def button_handler(v):
54+
get_app().set_result(v)
55+
56+
dialog = Dialog(
57+
title=title,
58+
body=Label(text=text, dont_extend_height=True),
59+
buttons=[Button(text=t, handler=functools.partial(button_handler, v)) for t, v in buttons],
60+
with_background=True)
61+
62+
return _run_dialog(dialog, style, async_=async_)
63+
64+
4565
def input_dialog(title='', text='', ok_text='OK', cancel_text='Cancel',
4666
completer=None, password=False, style=None, async_=False):
4767
"""

0 commit comments

Comments
 (0)