# Dialogs

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.

Learn Moreopen in new window

# Gtk.MessageDialog

Gtk.MessageDialog presents a dialog with some message text. It’s simply a convenience widget; you could construct the equivalent of Gtk.MessageDialog from Gtk.Dialog without too much effort, but Gtk.MessageDialog saves typing.

Learn Moreopen in new window

# Creating a dialog

To create the dialog we'll use GtkMessageDialog. GtkMessageDialog

let dialog = new Gtk.MessageDialog({
    title: 'Save?',
    text: 'Do you want to save your notes?',
    buttons: [Gtk.ButtonsType.NONE],
    parent: this,
    transient_for: this
})

dialog.add_button('Cancel', Gtk.ResponseType.CANCEL);
dialog.add_button('Close Without Saving', Gtk.ResponseType.NO);
dialog.add_button('Save Notes', Gtk.ResponseType.YES);
1
2
3
4
5
6
7
8
9
10
11

# Responding to the dialog

Now we'll need to respond to the dialog. To do this we'll use the return result of dialog.run().

let response = dialog.run();

if(response === Gtk.ResponseType.YES) {
  /* save code */
  /* exit code */
} else if (response === Gtk.ResponseType.NO) {
  /* exit code */
} else {
  /* do nothing */
}
1
2
3
4
5
6
7
8
9
10
Last Updated: 4/19/2021, 6:36:47 PM