PyGTK Dialog Class - PyGTK

What is PyGTK Dialog Class?

A Dialog widget is usually used as a pop-up window on top of a parental window. The objective of a Dialog is to gather some data from the user and direct it to the parent window. Dialog can be modal (where it blocks the parent frame) or modeless (dialog frame can be bypassed).

The Dialog widget of PyGTK archive is a window split vertically. In its top section, there is a gtk.VBox in which Label or Entry Widgets are packed. The bottom section is called action_area in which one or more buttons are placed. Two areas are separated by gtk.HSeparator.

gtk.Dialog class has the resulting constructor –


  • Title − Is the text appearing in the Title bar of the Dialog widget.
  • Parent − Is the reference to the toplevel window from which the dialog pops up.
  • Flag − Defines constants controlling operation of Dialog. The defined constants are −


If set, the dialog grabs all the keyboard events


If set, the dialog is destroyed when its parent is.


If set, there is no separator bar above the buttons.

What is a Button?

A Button is a tuple object comprising pairs of gtk.Button with a stock ID (or text) and its response IDs.
The response ID can be any number or one of the predefined Response ID constants −


The significant methods of the gtk.Dialog class are given below −

  • add_button() − Adds a button with the text specified by button_text (or a stock button, if button_text is a stock ID) in action_area.
  • response() − Emits the "response" signal with the value specified in response_id
  • run() − Displays the dialog and returns the response_id when delete_event gets emitted.
  • set_default_response() − Sets the last widget in the dialog's action area with the specified response_id as the default widget for the dialog.

gtk.Dialog widget emits the following signals −


This is emitted when the dialog is closed.


This is emitted when an action_area widget is activated (button "clicked"), the dialog receives a delete_event or the application calls the response() method.

Two buttons in action_area of Dialog widget use Stock IDs gtk.STOCK.CANCEL and gtk.STOCK_OK. They are associated with response IDs gtk. RESPONSE_REJECT and gtk. RESPONSE_ACCEPT individually. The Dialog is closed when any button is pressed. The run() methods returns corresponding response ID which may be utilized for further processing.

Following code displays a top level gtk.Window with a Button in it. When button is clicked, a Dialog appears with a label and two buttons.


Observe the following code –

The above code yields the resulting output –


Preconfigured Dialog Widgets

PyGTK API has a number of preconfigured Dialog widgets −

  • MessageDialog
  • AboutDialog
  • ColorSelectionDialog
  • FontSelectionDialog
  • FileChooserDialog

In order to validate the functioning of the overhead standard dialog in PyGTK, a menu with a menu item each invoking a dialog when clicked, is put in a gtk.Window in the following program. The Callback functions responding to activate the signal of each menu item is listed. You can also understand the explanation provided for each type of dialog widget.


Observe the resulting code –

The above code will generate the resulting output –


All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd Protection Status

PyGTK Topics