Question

Is it possible to create a message box (with wx.MessageDialog or anything else) without parent window ?

For example, I sometimes may want to display an error message before the GUI has really started. Then I would need to be able to display a message box before having a parent window :

With parent = None, this doesn't work :

wx.MessageDialog(parent, 'This is a message box.', 'Test', wx.OK | wx.ICON_INFORMATION).ShowModal()

How to display a message box without a parent window ?

Was it helpful?

Solution 2

It should work, try this:

import wx

app = wx.App()

wx.MessageDialog(None, 'This is a message box.', 'Test', wx.OK | wx.ICON_INFORMATION).ShowModal()

frame = wx.Frame(None)
frame.Center()
frame.Show()

app.MainLoop()

OTHER TIPS

Just saw this old question and wanted to answer it, better late than never:

By default, the main application window is used as the dialog parent even if no parent is specified explicitly, because this is what you want in 99% of cases -- modal dialogs without parent/owner window are quite unusual. If you really, really need to prevent the dialog from having a parent, you must use wx.DIALOG_NO_PARENT style explicitly.

I know this is an old question, but I believe that parent=None does not work as one might expect. Consider the example above, but with the wx.Frame shown first, and the wx.MessageDialog after, like so:

import wx

app = wx.App()

frame = wx.Frame(None)
frame.Center()
frame.Show()

wx.MessageDialog(None, 'This is a message box.', 'Test', wx.OK | wx.ICON_INFORMATION).ShowModal()

app.MainLoop()

The result is a wx.Frame with the wx.MessageDialog shown on top of it (as expected), but the wx.Frame cannot be resized or draged around the screen (not expected). The wx.MessageDialog can be draged around the screen, but the wx.MessageDialog moves with it (not expected). The two frames clearly do not work independent of each other, and it seems the wx.MessageDialog is owned by the wx.Frame. I therefore think that wxPython applies some magic that is not obvious; at least I couldn't see anything in the docs.

Without parent frame.

There seems no need to use a frame for showing a 'stand alone' dialog. This works fine. (Tested only on Win10.)

Apparently wxpython takes the dialog, which is of course also just a window, as the 'frame' to display.

import wx

# -------------------------------------------------------
def wx_ask_question_windowed(question, caption):

    app = wx.App()

    dlg = wx.MessageDialog(None, question, caption, wx.YES_NO | wx.ICON_INFORMATION)
    dlg.Center()

    dlg_result = dlg.ShowModal()
    result = dlg_result == wx.ID_YES

    dlg.Destroy()

    app.MainLoop()

    app.Destroy()

    return result


# ==============================================================
def main():

    if wx_ask_question_windowed('Do you like this?', 'A windowed question'):
        print('You like it')
    else:
        print("You don't like it")


# ==============================================================
if __name__ == '__main__':
    main()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top