def yes_no_dialog(title='', text='', yes_text='Yes', no_text='No', style=None, async_=False): """ Display a Yes/No dialog. Return a boolean. """ def yes_handler(): get_app().set_return_value(True) def no_handler(): get_app().set_return_value(False) dialog = Dialog(title=title, body=Label(text=text, dont_extend_height=True), buttons=[ Button(text=yes_text, handler=yes_handler), Button(text=no_text, handler=no_handler), ], with_background=True) return _run_dialog(dialog, style, async_=async_)
def radiolist_dialog(title='', text='', ok_text='Ok', cancel_text='Cancel', values=None, style=None, async_=False): """ Display a simple message box and wait until the user presses enter. """ def ok_handler(): get_app().set_return_value(radio_list.current_value) radio_list = RadioList(values) dialog = Dialog(title=title, body=HSplit([ Label(text=text, dont_extend_height=True), radio_list, ], padding=1), buttons=[ Button(text=ok_text, handler=ok_handler), Button(text=cancel_text, handler=_return_none), ], with_background=True) return _run_dialog(dialog, style, async_=async_)
def message_dialog(title='', text='', ok_text='Ok', style=None, async_=False): """ Display a simple message box and wait until the user presses enter. """ dialog = Dialog(title=title, body=Label(text=text, dont_extend_height=True), buttons=[ Button(text=ok_text, handler=_return_none), ], with_background=True) return _run_dialog(dialog, style, async_=async_)
def progress_dialog(title='', text='', run_callback=None, style=None, async_=False): """ :param run_callback: A function that receives as input a `set_percentage` function and it does the work. """ assert callable(run_callback) progressbar = ProgressBar() text_area = TextArea( focussable=False, # Prefer this text area as big as possible, to avoid having a window # that keeps resizing when we add text to it. height=D(preferred=10**10)) dialog = Dialog(body=HSplit([ Box(Label(text=text)), Box(text_area, padding=D.exact(1)), progressbar, ]), title=title, with_background=True) app = _create_app(dialog, style) def set_percentage(value): progressbar.percentage = int(value) app.invalidate() def log_text(text): text_area.buffer.insert_text(text) app.invalidate() # Run the callback in the executor. When done, set a return value for the # UI, so that it quits. def start(): try: run_callback(set_percentage, log_text) finally: app.set_return_value(None) run_in_executor(start) if async_: return app.run_async() else: return app.run()
def input_dialog(title='', text='', ok_text='OK', cancel_text='Cancel', completer=None, password=False, style=None, async_=False): """ Display a text input box. Return the given text, or None when cancelled. """ def accept(): get_app().layout.focus(ok_button) def ok_handler(): get_app().set_return_value(textfield.text) ok_button = Button(text=ok_text, handler=ok_handler) cancel_button = Button(text=cancel_text, handler=_return_none) textfield = TextArea(multiline=False, password=password, completer=completer, accept_handler=accept) dialog = Dialog(title=title, body=HSplit([ Label(text=text, dont_extend_height=True), textfield, ], padding=D(preferred=1, max=1)), buttons=[ok_button, cancel_button], with_background=True) return _run_dialog(dialog, style, async_=async_)