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).
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
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.
^^
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. ^^
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
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. |
|
Sets the aggressiveness of search for buttons and sizers to be in the non-scrolling part of a layout-adapted dialog. |
|
Sets the adaptation mode, overriding the global adaptation flag. |
|
A static function for setting the current layout adapter object, returning the old adapter. |
|
Sets the return code for this window. |
|
Hides or shows the dialog. |
|
Shows an application-modal dialog. |
|
Shows a dialog modal to the parent top level window only. |
|
See |
|
See |
|
See |
|
See |
|
See |
wx.
Dialog
(TopLevelWindow)¶Possible constructors:
Dialog()
Dialog(parent, id=ID_ANY, title="", pos=DefaultPosition,
size=DefaultSize, style=DEFAULT_DIALOG_STYLE, name=DialogNameStr)
A dialog box is a window with a title bar and sometimes a system menu, which can be moved around the screen.
__init__
(self, *args, **kw)¶__init__ (self)
Default constructor.
__init__ (self, parent, id=ID_ANY, title=””, pos=DefaultPosition, size=DefaultSize, style=DEFAULT_DIALOG_STYLE, name=DialogNameStr)
Constructor.
parent (wx.Window) – Can be None
, a frame or another dialog box.
id (wx.WindowID) – An identifier for the dialog. A value of -1 is taken to mean a default.
title (string) – The title of the dialog.
pos (wx.Point) – The dialog position. The value DefaultPosition indicates a default position, chosen by either the windowing system or wxWidgets, depending on platform.
size (wx.Size) – The dialog size. The value DefaultSize indicates a default size, chosen by either the windowing system or wxWidgets, depending on platform.
style (long) – The window style.
name (string) – Used to associate a name with the window, allowing the application user to set Motif resource values for individual dialog boxes.
AddMainButtonId
(self, id)¶Adds an identifier to be regarded as a main button for the non-scrolling area of a dialog.
id (wx.WindowID) –
See also
Automatic Scrolled Dialogs (for more on layout adaptation)
CanDoLayoutAdaptation
(self)¶Returns True
if this dialog can and should perform layout adaptation using DoLayoutAdaptation
, usually if the dialog is too large to fit on the display.
bool
See also
Automatic Scrolled Dialogs (for more on layout adaptation)
Centre
(self, direction=BOTH)¶Centres the dialog box on the display.
direction (int) – May be wx.HORIZONTAL
, wx.VERTICAL
or wx.BOTH
.
Create
(self, parent, id=ID_ANY, title="", pos=DefaultPosition, size=DefaultSize, style=DEFAULT_DIALOG_STYLE, name=DialogNameStr)¶Used for two-step dialog box construction.
bool
See also
CreateButtonSizer
(self, flags)¶Creates a sizer with standard buttons.
flags is a bit list of the following flags: wx.OK
, wx.CANCEL
, wx.YES
, wx.NO
, wx.APPLY
, wx.CLOSE
, wx.HELP
, wx.NO_DEFAULT
.
The sizer lays out the buttons in a manner appropriate to the platform.
This function uses CreateStdDialogButtonSizer
internally for most platforms but doesn’t create the sizer at all for the platforms with hardware buttons (such as smartphones) for which it sets up the hardware buttons appropriately and returns None
, so don’t forget to test that the return value is valid before using it.
flags (long) –
CreateSeparatedButtonSizer
(self, flags)¶Creates a sizer with standard buttons using CreateButtonSizer
separated from the rest of the dialog contents by a horizontal wx.StaticLine.
This is a combination of CreateButtonSizer
and CreateSeparatedSizer
.
flags (long) –
Note
Just like CreateButtonSizer
, this function may return None
if no buttons were created.
CreateSeparatedSizer
(self, sizer)¶Returns the sizer containing the given one with a separating wx.StaticLine if necessarily.
This function is useful for creating the sizer containing footer-like contents in dialog boxes. It will add a separating static line only if it conforms to the current platform convention (currently it is not added under Mac where the use of static lines for grouping is discouraged and is added elsewhere).
sizer (wx.Sizer) – The sizer to wrap, must be not None
.
The sizer wrapping the input one or possibly the input sizer itself if no wrapping is necessary.
New in version 2.9.2.
CreateStdDialogButtonSizer
(self, flags)¶Creates a wx.StdDialogButtonSizer with standard buttons.
flags is a bit list of the following flags: wx.OK
, wx.CANCEL
, wx.YES
, wx.NO
, wx.APPLY
, wx.CLOSE
, wx.HELP
, wx.NO_DEFAULT
.
The sizer lays out the buttons in a manner appropriate to the platform.
flags (long) –
CreateTextSizer
(self, message, widthMax=-1)¶Splits text up at newlines and places the lines into wx.StaticText objects with the specified maximum width in a vertical wx.BoxSizer.
If widthMax has its default value of -1, only explicit new line characters in message are taken into account. Otherwise, lines are broken either after a new line or wrapped, at word boundary, if their width would become bigger than the specified maximal width.
message (string) – The text to be displayed.
widthMax (int) – Specifies the text’s maximum width (this argument is available since version 3.1.1, previous versions always behaved as if the maximal width of -1 was specified).
See also
DoLayoutAdaptation
(self)¶Performs layout adaptation, usually if the dialog is too large to fit on the display.
bool
See also
Automatic Scrolled Dialogs (for more on layout adaptation)
EnableLayoutAdaptation
(enable)¶A static function enabling or disabling layout adaptation for all dialogs.
enable (bool) –
See also
Automatic Scrolled Dialogs (for more on layout adaptation)
EndModal
(self, retCode)¶Ends a modal dialog, passing a value to be returned from the ShowModal
invocation.
retCode (int) – The value that should be returned by ShowModal.
See also
GetAffirmativeId
(self)¶Gets the identifier of the button which works like standard wx.OK
button in this dialog.
int
See also
GetClassDefaultAttributes
(variant=WINDOW_VARIANT_NORMAL)¶variant (WindowVariant) –
GetContentWindow
(self)¶Override this to return a window containing the main content of the dialog.
This is particularly useful when the dialog implements pages, such as wx.adv.PropertySheetDialog, and allows the layout adaptation code to know that only the pages need to be made scrollable.
GetEscapeId
(self)¶Gets the identifier of the button to map presses of ESC
button to.
int
See also
GetLayoutAdaptationDone
(self)¶Returns True
if the dialog has been adapted, usually by making it scrollable to work with a small display.
bool
See also
Automatic Scrolled Dialogs (for more on layout adaptation)
GetLayoutAdaptationLevel
(self)¶Gets a value representing the aggressiveness of search for buttons and sizers to be in the non-scrolling part of a layout-adapted dialog.
Zero switches off adaptation, and 3 allows search for standard buttons anywhere in the dialog.
int
See also
Automatic Scrolled Dialogs (for more on layout adaptation)
GetLayoutAdaptationMode
(self)¶Gets the adaptation mode, overriding the global adaptation flag.
See also
Automatic Scrolled Dialogs (for more on layout adaptation)
GetLayoutAdapter
()¶A static function getting the current layout adapter object.
See also
Automatic Scrolled Dialogs (for more on layout adaptation)
GetMainButtonIds
(self)¶Returns an array of identifiers to be regarded as the main buttons for the non-scrolling area of a dialog.
list of integers
See also
Automatic Scrolled Dialogs (for more on layout adaptation)
GetReturnCode
(self)¶Gets the return code for this window.
int
Note
A return code is normally associated with a modal dialog, where ShowModal
returns a code to the application.
See also
Iconize
(self, iconize=True)¶Iconizes or restores the dialog.
Windows only.
iconize (bool) – If True
, iconizes the dialog box; if False
, shows and restores it.
Note
Note that in Windows, iconization has no effect since dialog boxes cannot be iconized. However, applications may need to explicitly restore dialog boxes under Motif which have user-iconizable frames, and under Windows calling Iconize(false) will bring the window to the front, as does Show(true).
IsIconized
(self)¶Returns True
if the dialog box is iconized.
Windows only.
bool
Note
Always returns False
under Windows since dialogs cannot be iconized.
IsLayoutAdaptationEnabled
()¶A static function returning True
if layout adaptation is enabled for all dialogs.
bool
See also
Automatic Scrolled Dialogs (for more on layout adaptation)
IsMainButtonId
(self, id)¶Returns True
if id is in the array of identifiers to be regarded as the main buttons for the non-scrolling area of a dialog.
id (wx.WindowID) –
bool
Availability
Only available for MSW.
See also
Automatic Scrolled Dialogs (for more on layout adaptation)
IsModal
(self)¶Returns True
if the dialog box is modal, False
otherwise.
bool
SetAffirmativeId
(self, id)¶Sets the identifier to be used as wx.OK
button.
When the button with this identifier is pressed, the dialog calls wx.Window.Validate
and wx.Window.TransferDataFromWindow
and, if they both return True
, closes the dialog with the affirmative id return code.
Also, when the user presses a hardware wx.OK
button on the devices having one or the special wx.OK
button in the PocketPC title bar, an event with this id is generated.
By default, the affirmative id is wx.ID_OK
.
id (int) –
See also
SetEscapeId
(self, id)¶Sets the identifier of the button which should work like the standard “Cancel” button in this dialog.
When the button with this id is clicked, the dialog is closed. Also, when the user presses ESC
key in the dialog or closes the dialog using the close button in the title bar, this is mapped to the click of the button with the specified id.
By default, the escape id is the special value wx.ID_ANY
meaning that wx.ID_CANCEL
button is used if it’s present in the dialog and otherwise the button with GetAffirmativeId
is used. Another special value for id is wx.ID_NONE
meaning that ESC
presses should be ignored. If any other value is given, it is interpreted as the id of the button to map the escape key to.
id (int) –
Note
This method should be used for custom modal dialog implemented in wxWidgets itself, native dialogs such as wx.MessageDialog or wx.FileDialog, handle ESC
presses in their own way which cannot be customized.
SetIcon
(self, icon)¶Sets the icon for this dialog.
icon (wx.Icon) – The icon to associate with this dialog.
See also
SetIcons
(self, icons)¶Sets the icons for this dialog.
icons (wx.IconBundle) – The icons to associate with this dialog.
See also
SetLayoutAdaptationDone
(self, done)¶Marks the dialog as having been adapted, usually by making it scrollable to work with a small display.
done (bool) –
See also
Automatic Scrolled Dialogs (for more on layout adaptation)
SetLayoutAdaptationLevel
(self, level)¶Sets the aggressiveness of search for buttons and sizers to be in the non-scrolling part of a layout-adapted dialog.
Zero switches off adaptation, and 3 allows search for standard buttons anywhere in the dialog.
level (int) –
See also
Automatic Scrolled Dialogs (for more on layout adaptation)
SetLayoutAdaptationMode
(self, mode)¶Sets the adaptation mode, overriding the global adaptation flag.
mode (DialogLayoutAdaptationMode) –
See also
wx.DialogLayoutAdaptationMode, Automatic Scrolled Dialogs (for more on layout adaptation)
SetLayoutAdapter
(adapter)¶A static function for setting the current layout adapter object, returning the old adapter.
If you call this, you should delete the old adapter object.
adapter (wx.DialogLayoutAdapter) –
SetReturnCode
(self, retCode)¶Sets the return code for this window.
A return code is normally associated with a modal dialog, where ShowModal
returns a code to the application. The function EndModal
calls SetReturnCode
.
retCode (int) – The integer return code, usually a control identifier.
See also
Show
(self, show=True)¶Hides or shows the dialog.
The preferred way of dismissing a modal dialog is to use EndModal
.
show (bool) – If True
, the dialog box is shown and brought to the front, otherwise the box is hidden. If False
and the dialog is modal, control is returned to the calling program.
bool
ShowModal
(self)¶Shows an application-modal dialog.
Program flow does not return until the dialog has been dismissed with EndModal
.
Notice that it is possible to call ShowModal
for a dialog which had been previously shown with Show
, this allows making an existing modeless dialog modal. However ShowModal
can’t be called twice without intervening EndModal
calls.
Note that this function creates a temporary event loop which takes precedence over the application’s main event loop (see wx.EventLoopBase) and which is destroyed when the dialog is dismissed. This also results in a call to wx.App.ProcessPendingEvents
.
int
The value set with SetReturnCode
.
See also
ShowWindowModal
, ShowWindowModalThenDo
, EndModal
, GetReturnCode
, SetReturnCode
ShowWindowModal
(self)¶Shows a dialog modal to the parent top level window only.
Unlike ShowModal
, dialogs shown with this function only prevent the user from interacting with their parent frame only but not with the rest of the application. They also don’t block the program execution but instead return immediately, as Show
, and generate a wxEVT_WINDOW_MODAL_DIALOG_CLOSED event ( wx.WindowModalDialogEvent) later when the dialog is closed.
Currently this function is only fully implemented in wxOSX ports, under the other platforms it behaves like ShowModal
(but also sends the above mentioned event).
New in version 2.9.0.
See also
wx.WindowModalDialogEvent, ShowWindowModalThenDo
__enter__
(self)¶__exit__
(self, exc_type, exc_val, exc_tb)¶AffirmativeId
¶See GetAffirmativeId
and SetAffirmativeId
ContentWindow
¶See GetContentWindow
EscapeId
¶See GetEscapeId
and SetEscapeId
LayoutAdaptationDone
¶LayoutAdaptationLevel
¶LayoutAdaptationMode
¶MainButtonIds
¶See GetMainButtonIds
ReturnCode
¶See GetReturnCode
and SetReturnCode