wx.Dialog¶A dialog box is a window with a title bar and sometimes a system menu, which can be moved around the screen.
It can contain controls and other windows and is often used to allow the user to make some choice or to answer a question.
Dialogs can be made scrollable, automatically, for computers with low resolution screens: please see Automatic Scrolled Dialogs for further details.
Dialogs usually contain either a single button allowing to close the dialog or two buttons, one accepting the changes and the other one discarding them (such button, if present, is automatically activated if the user presses the “Esc” key). By default, buttons with the standard wx.ID_OK and wx.ID_CANCEL identifiers behave as expected. Starting with wxWidgets 2.7 it is also possible to use a button with a different identifier instead, see SetAffirmativeId and SetEscapeId.
Also notice that the CreateButtonSizer should be used to create the buttons appropriate for the current platform and positioned correctly (including their order which is platform-dependent).
Modal and Modeless¶There are two kinds of dialog, modal and modeless. A modal dialog blocks program flow and user input on other windows until it is dismissed, whereas a modeless dialog behaves more like a frame in that program flow continues, and input in other windows is still possible. To show a modal dialog you should use the ShowModal method while to show a dialog modelessly you simply use Show, just as with frames. Note that the modal dialog is one of the very few examples of Window-derived objects which may be created on the stack and not on the heap. In other words, while most windows would be created like this:
# In Python we don't have to worry about the stack vs. the heap, however
# that means that dialogs do need to be destroyed. The typical pattern for
# dialog usage looks something like this:
def AskUser(self):
try:
dlg = MyAskDialog(self)
if dlg.ShowModal() == wx.ID_OK:
# do something here
print('Hello')
else:
# handle dialog being cancelled or ended by some other button
...
finally:
# explicitly cause the dialog to destroy itself
dlg.Destroy()
You can achieve the same result with dialogs by using simpler code:
# Things can be made a little simpler in Python by using the dialog as a
# context manager, using the with statement, like this:
def AskUser(self):
with MyAskDialog(self) as dlg:
if dlg.ShowModal() == wx.ID_OK:
# do something here
print('Hello')
else:
# handle dialog being cancelled or ended by some other button
...
# The dialog is automatically destroyed on exit from the context manager
Window Styles¶An application can define a wx.CloseEvent handler for the dialog to respond to system close events. This class supports the following styles:
wx.CAPTION: Puts a caption on the dialog box.
wx.DEFAULT_DIALOG_STYLE: Equivalent to a combination of wx.CAPTION, wx.CLOSE_BOX and wx.SYSTEM_MENU (the last one is not used under Unix).
wx.RESIZE_BORDER: Display a resizable frame around the window.
wx.SYSTEM_MENU: Display a system menu.
wx.CLOSE_BOX: Displays a close box on the frame.
wx.MAXIMIZE_BOX: Displays a maximize box on the dialog.
wx.MINIMIZE_BOX: Displays a minimize box on the dialog.
THICK_FRAME: Display a thick frame around the window.
wx.STAY_ON_TOP: The dialog stays on top of all other windows.
NO_3D: This style is obsolete and doesn’t do anything any more, don’t use it in any new code.
wx.DIALOG_NO_PARENT: By default, a dialog created with a None parent window will be given the application’s top level window as parent. Use this style to prevent this from happening and create an orphan dialog. This is not recommended for modal dialogs.
wx.DIALOG_EX_CONTEXTHELP: Under Windows, puts a query button on the caption. When pressed, Windows will go into a context-sensitive help mode and wxWidgets will send a wxEVT_HELP event if the user clicked on an application window. Note that this is an extended style and must be set by calling SetExtraStyle before Create is called (two-step construction).
wx.DIALOG_EX_METAL: On macOS, frames with this style will be shown with a metallic look. This is an extra style.
Under Unix or Linux, MWM (the Motif Window Manager) or other window managers recognizing the MHM hints should be running for any of these styles to have an effect.
Events Emitted by this Class¶Handlers bound for the following event types will receive one of the wx.CloseEvent parameters.
EVT_CLOSE: The dialog is being closed by the user or programmatically (see wx.Window.Close ). The user may generate this event clicking the close button (typically the ‘X’ on the top-right of the title bar) if it’s present (see the CLOSE_BOX style).
EVT_INIT_DIALOG: Process a wxEVT_INIT_DIALOG event. See wx.InitDialogEvent.
See also
Class Hierarchy¶
Inheritance diagram for class Dialog:
Control Appearance¶
Known Subclasses¶wx.ColourDialog, wx.CredentialEntryDialog , wx.DirDialog, wx.FileDialog, wx.FindReplaceDialog, wx.FontDialog, wx.GenericProgressDialog, wx.html.HtmlHelpDialog, wx.MessageDialog, wx.MultiChoiceDialog, wx.NumberEntryDialog, wx.propgrid.PGArrayEditorDialog, wx.PrintAbortDialog, wx.adv.PropertySheetDialog, wx.RearrangeDialog, wx.richtext.RichTextStyleOrganiserDialog, wx.SingleChoiceDialog, wx.richtext.SymbolPickerDialog, wx.TextEntryDialog, wx.adv.Wizard
Methods Summary¶Default constructor. |
|
Adds an identifier to be regarded as a main button for the non-scrolling area of a dialog. |
|
Returns |
|
Centres the dialog box on the display. |
|
Used for two-step dialog box construction. |
|
Creates a sizer with standard buttons. |
|
Creates a sizer with standard buttons using |
|
Returns the sizer containing the given one with a separating wx.StaticLine if necessarily. |
|
Creates a wx.StdDialogButtonSizer with standard buttons. |
|
Splits text up at newlines and places the lines into wx.StaticText objects with the specified maximum width in a vertical wx.BoxSizer. |
|
Performs layout adaptation, usually if the dialog is too large to fit on the display. |
|
A static function enabling or disabling layout adaptation for all dialogs. |
|
Ends a modal dialog, passing a value to be returned from the |
|
Gets the identifier of the button which works like standard |
|
Override this to return a window containing the main content of the dialog. |
|
Gets the identifier of the button to map presses of |
|
Returns |
|
Gets a value representing the aggressiveness of search for buttons and sizers to be in the non-scrolling part of a layout-adapted dialog. |
|
Gets the adaptation mode, overriding the global adaptation flag. |
|
A static function getting the current layout adapter object. |
|
Returns an array of identifiers to be regarded as the main buttons for the non-scrolling area of a dialog. |
|
Gets the return code for this window. |
|
Iconizes or restores the dialog. |
|
Returns |
|
A static function returning |
|
Returns |
|
Returns |
|
Sets the identifier to be used as |
|
Sets the identifier of the button which should work like the standard “Cancel” button in this dialog. |
|
Sets the icon for this dialog. |
|
Sets the icons for this dialog. |
|
Marks the dialog as having been adapted, usually by making it scrollable to work with a small display. |
|