9.5. Dialogs

The Dialog widget is very simple, and is actually just a window with a few things pre-packed into it for you. It simply creates a window, and then packs a VBox into the top, which contains a separator and then an HBox called the "action_area".

The Dialog widget can be used for pop-up messages to the user, and other similar tasks. It is really basic, and there is only one function for the dialog box, which is:

  dialog = gtk.Dialog(title=None, parent=None, flags=0, buttons=None)

where title is the text to be used in the titlebar, parent is the main application window and flags set various modes of operation for the dialog:

  DIALOG_MODAL - make the dialog modal
  DIALOG_DESTROY_WITH_PARENT - destroy dialog when its parent is destroyed
  DIALOG_NO_SEPARATOR - omit the separator between the vbox and the action_area  

The buttons argument is a tuple of button text and response pairs. All arguments have defaults and can be specified using keywords.

This will create the dialog box, and it is now up to you to use it. You could pack a button in the action_area:

  button = ...
  dialog.action_area.pack_start(button, TRUE, TRUE, 0)
  button.show()

And you could add to the vbox area by packing, for instance, a label in it, try something like this:

  label = gtk.Label("Dialogs are groovy")
  dialog.vbox.pack_start(label, TRUE, TRUE, 0)
  label.show()

As an example in using the dialog box, you could put two buttons in the action_area, a Cancel button and an Ok button, and a label in the vbox area, asking the user a question or giving an error, etc. Then you could attach a different signal to each of the buttons and perform the operation the user selects.

If the simple functionality provided by the default vertical and horizontal boxes in the two areas doesn't give you enough control for your application, then you can simply pack another layout widget into the boxes provided. For example, you could pack a table into the vertical box.

9.5.1. Message Dialogs

A message dialog is a specialization of the already rather simple *Dialog* widget for displaying standardized error, question, and information popups. Invoke it like this:

message = gtk.MessageDialog(parent=None, 
                            flags=0, 
                            type=gtk.MESSAGE_INFO, 
                            buttons=gtk.BUTTONS_NONE, 
                            message_format=None)

The type flag selects a stock icon to be displayed in the message:

MESSAGE_INFO - information message
MESSAGE_WARNING - warning (or recoverable error) message
MESSAGE_QUESTION - question that can be answered with a button click
MESSAGE_ERROR - error message

To set the text for the message, feed it a Pango markup string. As a matter of style, you probably want to stick to relatively terse, one-sentence messages when using this widget.

message.set_markup("Sample message, could contain pango markup")

Here's an example program, message.py

     1	#!/usr/bin/env python
       
     2	# message.py -- example program illustrating use of message dialog widget 
       
     3	import pygtk
     4	pygtk.require('2.0')
     5	import gtk
       
     6	if __name__ == "__main__":
     7	    message = gtk.MessageDialog(type=gtk.MESSAGE_ERROR, buttons=gtk.BUTTONS_OK)
     8	    message.set_markup("An example error popup.")
     9	    message.run()

We used run() here to make the dialog modal; we could have achieved the same result by setting flags to DIALOG_MODAL, and doing this instead:

    message.show()
    gtk.main()