Gtk.Dialog

g Atk.ImplementorIface Atk.ImplementorIface Gtk.Widget Gtk.Widget Atk.ImplementorIface->Gtk.Widget GObject.GInterface GObject.GInterface GObject.GInterface->Atk.ImplementorIface Gtk.Buildable Gtk.Buildable GObject.GInterface->Gtk.Buildable GObject.InitiallyUnowned GObject.InitiallyUnowned GObject.InitiallyUnowned->Gtk.Widget GObject.Object GObject.Object GObject.Object->GObject.InitiallyUnowned Gtk.Bin Gtk.Bin Gtk.Window Gtk.Window Gtk.Bin->Gtk.Window Gtk.Buildable->Gtk.Widget Gtk.Container Gtk.Container Gtk.Container->Gtk.Bin Gtk.Dialog Gtk.Dialog Gtk.Widget->Gtk.Container Gtk.Window->Gtk.Dialog

Subclasses:

Gtk.AboutDialog, Gtk.AppChooserDialog, Gtk.ColorChooserDialog, Gtk.ColorSelectionDialog, Gtk.FileChooserDialog, Gtk.FontChooserDialog, Gtk.FontSelectionDialog, Gtk.MessageDialog, Gtk.RecentChooserDialog

Methods

Inherited:

Gtk.Window (119), Gtk.Bin (1), Gtk.Container (35), Gtk.Widget (278), GObject.Object (37), Gtk.Buildable (10)

Structs:

Gtk.ContainerClass (5), Gtk.WidgetClass (12), GObject.ObjectClass (5)

class

new ()

add_action_widget (child, response_id)

add_button (button_text, response_id)

add_buttons (*args)

get_action_area ()

get_content_area ()

get_header_bar ()

get_response_for_widget (widget)

get_widget_for_response (response_id)

response (response_id)

run ()

set_alternative_button_order_from_array (new_order)

set_default_response (response_id)

set_response_sensitive (response_id, setting)

Virtual Methods

Inherited:

Gtk.Window (5), Gtk.Container (10), Gtk.Widget (82), GObject.Object (7), Gtk.Buildable (10)

do_close ()

do_response (response_id)

Properties

Inherited:

Gtk.Window (33), Gtk.Container (3), Gtk.Widget (39)

Name

Type

Flags

Short Description

use-header-bar

int

r/w/co

Use Header Bar for actions.

Style Properties

Inherited:

Gtk.Window (2), Gtk.Widget (17)

Name

Type

Default

Flags

Short Description

action-area-border

int

5

r

Width of border around the button area at the bottom of the dialog

button-spacing

int

6

r

Spacing between buttons

content-area-border

int

2

r

Width of border around the main dialog area

content-area-spacing

int

0

r

Spacing between elements of the main dialog area

Signals

Inherited:

Gtk.Window (5), Gtk.Container (4), Gtk.Widget (69), GObject.Object (1)

Name

Short Description

close

The ::close signal is a keybinding signal which gets emitted when the user uses a keybinding to close the dialog.

response

Emitted when an action widget is clicked, the dialog receives a delete event, or the application programmer calls Gtk.Dialog.response().

Fields

Inherited:

Gtk.Window (5), Gtk.Container (4), Gtk.Widget (69), GObject.Object (1)

Name

Type

Access

Description

window

Gtk.Window

r

Class Details

class Gtk.Dialog(*args, **kwargs)
Bases:

Gtk.Window

Abstract:

No

Structure:

Gtk.DialogClass

Dialog boxes are a convenient way to prompt the user for a small amount of input, e.g. to display a message, ask a question, or anything else that does not require extensive effort on the user’s part.

GTK+ treats a dialog as a window split vertically. The top section is a Gtk.VBox, and is where widgets such as a Gtk.Label or a Gtk.Entry should be packed. The bottom area is known as the “action area”. This is generally used for packing buttons into the dialog which may perform functions such as cancel, ok, or apply.

Gtk.Dialog boxes are created with a call to Gtk.Dialog.new() or gtk_dialog_new_with_buttons(). gtk_dialog_new_with_buttons() is recommended; it allows you to set the dialog title, some convenient flags, and add simple buttons.

If “dialog” is a newly created dialog, the two primary areas of the window can be accessed through Gtk.Dialog.get_content_area() and Gtk.Dialog.get_action_area(), as can be seen from the example below.

A “modal” dialog (that is, one which freezes the rest of the application from user input), can be created by calling Gtk.Window.set_modal() on the dialog. Use the GTK_WINDOW() macro to cast the widget returned from Gtk.Dialog.new() into a Gtk.Window. When using gtk_dialog_new_with_buttons() you can also pass the Gtk.DialogFlags.MODAL flag to make a dialog modal.

If you add buttons to Gtk.Dialog using gtk_dialog_new_with_buttons(), Gtk.Dialog.add_button(), Gtk.Dialog.add_buttons(), or Gtk.Dialog.add_action_widget(), clicking the button will emit a signal called Gtk.Dialog ::response with a response ID that you specified. GTK+ will never assign a meaning to positive response IDs; these are entirely user-defined. But for convenience, you can use the response IDs in the Gtk.ResponseType enumeration (these all have values less than zero). If a dialog receives a delete event, the Gtk.Dialog ::response signal will be emitted with a response ID of Gtk.ResponseType.DELETE_EVENT.

If you want to block waiting for a dialog to return before returning control flow to your code, you can call Gtk.Dialog.run(). This function enters a recursive main loop and waits for the user to respond to the dialog, returning the response ID corresponding to the button the user clicked.

For the simple dialog in the following example, in reality you’d probably use Gtk.MessageDialog to save yourself some effort. But you’d need to create the dialog contents manually if you had more than a simple message in the dialog.

An example for simple Gtk.Dialog usage:

// Function to open a dialog box with a message
void
quick_message (GtkWindow *parent, gchar *message)
{
 GtkWidget *dialog, *label, *content_area;
 GtkDialogFlags flags;

 // Create the widgets
 flags = GTK_DIALOG_DESTROY_WITH_PARENT;
 dialog = gtk_dialog_new_with_buttons ("Message",
                                       parent,
                                       flags,
                                       _("_OK"),
                                       GTK_RESPONSE_NONE,
                                       NULL);
 content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
 label = gtk_label_new (message);

 // Ensure that the dialog box is destroyed when the user responds

 g_signal_connect_swapped (dialog,
                           "response",
                           G_CALLBACK (gtk_widget_destroy),
                           dialog);

 // Add the label, and show everything we’ve added

 gtk_container_add (GTK_CONTAINER (content_area), label);
 gtk_widget_show_all (dialog);
}
Gtk.Dialog as Gtk.Buildable

The Gtk.Dialog implementation of the Gtk.Buildable interface exposes the vbox and action_area as internal children with the names “vbox” and “action_area”.

Gtk.Dialog supports a custom <action-widgets> element, which can contain multiple <action-widget> elements. The “response” attribute specifies a numeric response, and the content of the element is the id of widget (which should be a child of the dialogs action_area). To mark a response as default, set the “default“ attribute of the <action-widget> element to true.

Gtk.Dialog supports adding action widgets by specifying “action“ as the “type“ attribute of a <child> element. The widget will be added either to the action area or the headerbar of the dialog, depending on the “use-header-bar“ property. The response id has to be associated with the action widget using the <action-widgets> element.

An example of a Gtk.Dialog UI definition fragment:

<object class="GtkDialog" id="dialog1">
  <child type="action">
    <object class="GtkButton" id="button_cancel"/>
  </child>
  <child type="action">
    <object class="GtkButton" id="button_ok">
      <property name="can-default">True</property>
    </object>
  </child>
  <action-widgets>
    <action-widget response="cancel">button_cancel</action-widget>
    <action-widget response="ok" default="true">button_ok</action-widget>
  </action-widgets>
</object>
classmethod new()[source]
Returns:

the new dialog as a Gtk.Widget

Return type:

Gtk.Widget

Creates a new dialog box.

Widgets should not be packed into this Gtk.Window directly, but into the vbox and action_area, as described above.

add_action_widget(child, response_id)[source]
Parameters:
  • child (Gtk.Widget) – an activatable widget

  • response_id (int) – response ID for child

Adds an activatable widget to the action area of a Gtk.Dialog, connecting a signal handler that will emit the Gtk.Dialog ::response signal on the dialog when the widget is activated. The widget is appended to the end of the dialog’s action area. If you want to add a non-activatable widget, simply pack it into the action_area field of the Gtk.Dialog struct.

add_button(button_text, response_id)[source]
Parameters:
  • button_text (str) – text of button

  • response_id (int) – response ID for the button

Returns:

the Gtk.Button widget that was added

Return type:

Gtk.Widget

Adds a button with the given text and sets things up so that clicking the button will emit the Gtk.Dialog ::response signal with the given response_id. The button is appended to the end of the dialog’s action area. The button widget is returned, but usually you don’t need it.

add_buttons(*args)[source]

The add_buttons() method adds several buttons to the Gtk.Dialog using the button data passed as arguments to the method. This method is the same as calling the Gtk.Dialog.add_button() repeatedly. The button data pairs - button text (or stock ID) and a response ID integer are passed individually. For example:

dialog.add_buttons(Gtk.STOCK_OPEN, 42, "Close", Gtk.ResponseType.CLOSE)

will add “Open” and “Close” buttons to dialog.

get_action_area()[source]
Returns:

the action area

Return type:

Gtk.Box

Returns the action area of self.

New in version 2.14.

Deprecated since version 3.12: Direct access to the action area is discouraged; use Gtk.Dialog.add_button(), etc.

get_content_area()[source]
Returns:

the content area Gtk.Box.

Return type:

Gtk.Box

Returns the content area of self.

New in version 2.14.

get_header_bar()[source]
Returns:

the header bar

Return type:

Gtk.HeaderBar

Returns the header bar of self. Note that the headerbar is only used by the dialog if the Gtk.Dialog :use-header-bar property is True.

New in version 3.12.

get_response_for_widget(widget)[source]
Parameters:

widget (Gtk.Widget) – a widget in the action area of self

Returns:

the response id of widget, or Gtk.ResponseType.NONE if widget doesn’t have a response id set.

Return type:

int

Gets the response id of a widget in the action area of a dialog.

New in version 2.8.

get_widget_for_response(response_id)[source]
Parameters:

response_id (int) – the response ID used by the self widget

Returns:

the widget button that uses the given response_id, or None.

Return type:

Gtk.Widget or None

Gets the widget button that uses the given response ID in the action area of a dialog.

New in version 2.20.

response(response_id)[source]
Parameters:

response_id (int) – response ID

Emits the Gtk.Dialog ::response signal with the given response ID. Used to indicate that the user has responded to the dialog in some way; typically either you or Gtk.Dialog.run() will be monitoring the ::response signal and take appropriate action.

run()[source]
Returns:

response ID

Return type:

int

Blocks in a recursive main loop until the self either emits the Gtk.Dialog ::response signal, or is destroyed. If the dialog is destroyed during the call to Gtk.Dialog.run(), Gtk.Dialog.run() returns Gtk.ResponseType.NONE. Otherwise, it returns the response ID from the ::response signal emission.

Before entering the recursive main loop, Gtk.Dialog.run() calls Gtk.Widget.show() on the dialog for you. Note that you still need to show any children of the dialog yourself.

During Gtk.Dialog.run(), the default behavior of Gtk.Widget ::delete-event is disabled; if the dialog receives ::delete_event, it will not be destroyed as windows usually are, and Gtk.Dialog.run() will return Gtk.ResponseType.DELETE_EVENT. Also, during Gtk.Dialog.run() the dialog will be modal. You can force Gtk.Dialog.run() to return at any time by calling Gtk.Dialog.response() to emit the ::response signal. Destroying the dialog during Gtk.Dialog.run() is a very bad idea, because your post-run code won’t know whether the dialog was destroyed or not.

After Gtk.Dialog.run() returns, you are responsible for hiding or destroying the dialog if you wish to do so.

Typical usage of this function might be:

GtkWidget *dialog = gtk_dialog_new ();
// Set up dialog...

int result = gtk_dialog_run (GTK_DIALOG (dialog));
switch (result)
  {
    case GTK_RESPONSE_ACCEPT:
       // do_application_specific_something ();
       break;
    default:
       // do_nothing_since_dialog_was_cancelled ();
       break;
  }
gtk_widget_destroy (dialog);

Note that even though the recursive main loop gives the effect of a modal dialog (it prevents the user from interacting with other windows in the same window group while the dialog is run), callbacks such as timeouts, IO channel watches, DND drops, etc, will be triggered during a Gtk.Dialog.run() call.

set_alternative_button_order_from_array(new_order)[source]
Parameters:

new_order ([int]) – an array of response ids of self’s buttons

Sets an alternative button order. If the Gtk.Settings :gtk-alternative-button-order setting is set to True, the dialog buttons are reordered according to the order of the response ids in new_order.

See gtk_dialog_set_alternative_button_order() for more information.

This function is for use by language bindings.

New in version 2.6.

Deprecated since version 3.10: Deprecated

set_default_response(response_id)[source]
Parameters:

response_id (int) – a response ID

Sets the last widget in the dialog’s action area with the given response_id as the default widget for the dialog. Pressing “Enter” normally activates the default widget.

set_response_sensitive(response_id, setting)[source]
Parameters:
  • response_id (int) – a response ID

  • setting (bool) – True for sensitive

Calls gtk_widget_set_sensitive (widget, @setting) for each widget in the dialog’s action area with the given response_id. A convenient way to sensitize/desensitize dialog buttons.

do_close() virtual

Signal emitted when the user uses a keybinding to close the dialog.

do_response(response_id) virtual
Parameters:

response_id (int) – response ID

Emits the Gtk.Dialog ::response signal with the given response ID. Used to indicate that the user has responded to the dialog in some way; typically either you or Gtk.Dialog.run() will be monitoring the ::response signal and take appropriate action.

Signal Details

Gtk.Dialog.signals.close(dialog)
Signal Name:

close

Flags:

RUN_LAST, ACTION

Parameters:

dialog (Gtk.Dialog) – The object which received the signal

The ::close signal is a keybinding signal which gets emitted when the user uses a keybinding to close the dialog.

The default binding for this signal is the Escape key.

Gtk.Dialog.signals.response(dialog, response_id)
Signal Name:

response

Flags:

RUN_LAST

Parameters:
  • dialog (Gtk.Dialog) – The object which received the signal

  • response_id (int) – the response ID

Emitted when an action widget is clicked, the dialog receives a delete event, or the application programmer calls Gtk.Dialog.response(). On a delete event, the response ID is Gtk.ResponseType.DELETE_EVENT. Otherwise, it depends on which action widget was clicked.

Property Details

Gtk.Dialog.props.use_header_bar
Name:

use-header-bar

Type:

int

Default Value:

-1

Flags:

READABLE, WRITABLE, CONSTRUCT_ONLY

True if the dialog uses a Gtk.HeaderBar for action buttons instead of the action-area.

For technical reasons, this property is declared as an integer property, but you should only set it to True or False.

New in version 3.12.