Skip to content

Showing a simple message dialog in fullscreen apps - possible with simple code? #666

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
rolfschr opened this issue Jul 9, 2018 · 3 comments

Comments

@rolfschr
Copy link

rolfschr commented Jul 9, 2018

Hi,

I would like to show a message dialog in a full screen application but I can't find anything about this in the docs. I have seen what I want in examples/fullscreen/text-editor.py, specifically the 'About' dialog (I simply need something to confirm). The example contains a lot of code in order to get the dialog box appear and I was hoping to find something more simple like one or two function calls. Is that possible?
I thought message_dialog would do, but it doesn't due to the 'event loop is already running' exception (similar to #652).

@techtonik
Copy link
Contributor

I also look to fix this one:

  File "prompt_toolkit/shortcuts/dialogs.py", line 110, in message_dialog
    return _run_dialog(dialog, style, async_=async_)
  File "prompt_toolkit/shortcuts/dialogs.py", line 193, in _run_dialog
    return application.run()
  File "prompt_toolkit/application/application.py", line 682, in run
    return run()
  File "prompt_toolkit/application/application.py", line 655, in run
    run_until_complete(f, inputhook=inputhook)
  File "prompt_toolkit/eventloop/defaults.py", line 123, in run_until_complete
    return get_event_loop().run_until_complete(future, inputhook=inputhook)
  File "prompt_toolkit/eventloop/posix.py", line 57, in run_until_complete
    raise Exception('Event loop is already running')

Exception Event loop is already running

@jonathanslenders
Copy link
Member

jonathanslenders commented Jul 11, 2018

Hi @rolfschr,

Unfortunately, there is not really a shortcut to achieve this.
The reason is that the pop-up dialog should become part of the application layout in order to be displayed. There can be only one application running, so you have to add it to your main application. (The 'dialogs' shortcuts won't do that.) Further, the dialog shortcuts starts the event loop, this doesn't work in the middle of a running application, and causes the error you had.

You can have a FloatContainer as the root of the application, and add new Float objects to it whenever you need them.

from prompt_toolkit.layout import FloatContainer, Float
from prompt_toolkit.widgets import Dialog, Label, Button

def button_handler():
    pass

dialog = Dialog(
        title=title,
        body=Label(text="YOUR_TEXT", dont_extend_height=True),
        buttons=[
            Button(text="BUTTON_TEXT", handler=button_handler),
])

floatcontainer.floats.append(
    Float(content=dialog),  # optionally pass width and height.
)

@rolfschr
Copy link
Author

Hi,

thanks a lot. I was able to get the dialog as you described. Some hints that helped me:

  • The floatcontainer is actually also the object where the "main" content is placed (I first created a new FloatContainer instead of replacing my exising Window object). I checked examples/full-screen/simple-demos/floats.py and understood that stuff better than.
  • I made button_handler() contain floatcontainer.floats = [] to remove the button upon user press.
  • I also had to use layout.focus(floatcontainer) after floatcontainer.floats.append(...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants