How do I create an alert box

The three simplest ways of interacting with the user via a dialog box are wx.MessageDialog, which represents an alert box, wx.TextEntryDialog, which prompts the user to enter some short text, and wx.SingleChoiceDialog, which allows the user to select from a list of available options. The next three sections discuss these simple dialogs.

A message box dialog displays a short message and allows the user to press a button in response. Typically, message boxes are used to display important alerts, yes/no questions, or to ask the user to continue with or cancel some action. Figure 9.2 displays a typical message box.

Using a message box is quite simple. Listing 9.2 displays two ways of creating a message box.

Listing 9.2 Creating a message box import wx if _name_ == "_main_":

app = wx.PySimpleApp()

Figure 9.2 A standard message box, in a yes/ no configuration

dlg = wx.MessageDialog(None, "Is this explanation OK?", 'A Message Box', wx.YES_NO | wx.ICON_QUESTION) retCode = dlg.ShowModal() if (retCode == wx.ID_YES):

print "yes" else:

print "no" dlg.Destroy()

Using a function retCode = wx.MessageBox("Is this way easier?", "Via Function wx.YES_NO | wx.ICON_QUESTION)

Using a class

Listing 9.2 creates two message boxes, one after the other. The first method creates an instance of the class wx.MessageDialog, and displays it using ShowModal().

Using the wx.MessageDialog class

Using the constructor for the wx.MessageDialog, you can set the message and buttons for the dialog, as in the following:

wx.MessageDialog(parent, message, caption="Message box", style=wx.OK | wx.CANCEL, pos=wx.DefaultPosition)

The message argument is the text that is actually displayed inside the body of the dialog. If the message string contains \n newline characters, there will be line breaks in the text. The caption argument is displayed in the title box of the dialog. The pos argument allows you to specify where the dialog is displayed on the screen—under MS Windows, this argument is ignored.

The style flags for a wx.MessageDialog split into two types. The first type controls the buttons that display in the dialog. Table 9.1 describes these styles.

Table 9.1 Button styles for a wx.MessageDialog

Button Style



Include a cancel button. This button will have the ID value of wx.id_cancel.


In a wx.yes_no dialog, the No button is the default.


Include an OK button. This button will have the ID value of wx.id_ok.


In a wx.yes_no dialog, the Yes button is the default. This is the default behavior.


Include buttons labeled Yes and No, with the ID values of wx.id_yes and wx.id_no, respectively.

The second set of style flags controls the icon displayed next to the message text. Those styles are listed in Table 9.2.

Table 9.2 Icon styles for a wx.MessageDialog




An icon indicating an error.


An icon indicating an alert.


The same as wx.icon_error


The letter "i" information icon.


A question mark icon.

Finally, you can use the style wx.STAY_ON_TOP to display the dialog above any other windows in the system, including system windows and wxPython application windows.

As you can see in listing 9.2, the dialog is invoked using ShowModal(). Depending on the displayed buttons, the result is either wx.ID_OK, wx.ID_CANCEL, wx.ID_YES, or wx.ID_NO. As with other dialogs, you'll typically use the response value to control program execution in response to the dialog.

Using the wx.MessageBox() function

Line A of listing 9.2 displays a shorter method for invoking a message box. The convenience function wx.MessageBox() creates the dialog, calls ShowModal(), and returns, wx.YES, wx.NO, wx.CANCEL, or wx.OK. The signature of the function is simpler than the constructor for the MessageDialog object, as in:

wx.MessageBox(message, caption="Message", style=wx.OK)

In this example, message, caption, and style have the same meanings as in the constructor, and you can use all of the same style flags. As we'll see throughout this chapter, several of the predefined dialogs in wxPython also have convenience functions. As long as you are creating the dialogs for a single use, the mechanism you choose is a matter of preference. If you plan to hold onto the dialog to invoke it more than once, it may be preferable to instantiate yourself the object so you can hold onto the reference, although for simple dialogs such as these, the time saved is probably negligible.

To display a lot of text in your message box (i.e., an end-user license agreement display), you can use the wxPython-specific class wx.lib.dialogs.Scrolled-MessageDialog, which contains the following constructor:

wx.lib.dialogs.ScrolledMessageDialog(parent, msg, caption, pos=wx.wxDefaultPosition, size=(500,300))

This dialog doesn't use the native message box widget, it builds a dialog from other wxPython widgets. It only displays an OK button, and takes no further style information.

Was this article helpful?

+2 -2


    How can i use alert box in python?
    9 years ago
  • mehari
    How do you create a dialogue box in python programming?
    7 years ago
  • rachel
    How to create a message alerts in python?
    6 years ago
  • Katja
    How to make an alert with python?
    1 year ago
  • zainab thompson
    How to create a alert box in python with modal?
    10 months ago
    How to get result of messagebox in wxpython?
    9 months ago
  • pietro
    How to add alert box inside python progrms?
    7 months ago

Post a comment