Gtk has no attribute 'DIALOG_DESTROY_WITH_PARENT'

Question:

I am trying to create a working dialog box in Python 2.7/GTK+ 3 (PyGObject). I found an online tutorial which offered the following code…

md = Gtk.MessageDialog(window,
        Gtk.DIALOG_DESTROY_WITH_PARENT,
        Gtk.MESSAGE_INFO,
        Gtk.BUTTONS_CLOSE,
        msg)

response = md.run()

However, running this results in the error…

AttributeError: ‘gi.repository.Gtk’ object has no attribute
‘DIALOG_DESTROY_WITH_PARENT’

I’m fairly sure this has to do with the fact that the above code worked on PyGtk (GTK 2). How do I get this working?

Asked By: CodeMouse92

||

Answers:

After a little bit of research, I found that, yes, this is due to a change in library structure from PyGTK to PyGObject. (Read the documentation for how to work with dialogs, and see line 27 of the example at that link’s bookmark.)

The enumeration Gtk.DIALOG_DESTROY_WITH_PARENT does not appear to exist in PyGObject, as the documentation suggests passing a 0 directly.

Beyond that, Gtk.MESSAGE_INFO has been moved to Gtk.MessageType.INFO, and GTK.BUTTONS_CLOSE has been moved to Gtk.ButtonsType.CLOSE.

This may be bright-blazingly obvious to some, but Gtk isn’t exactly famous for their documentation, so this is for anyone who might have been fighting with this for a while as I did.

Answered By: CodeMouse92

As shown in this Flags documentation, there is a Gtk.DialogFlags.DESTROY_WITH_PARENT flag available.

Answered By: theDrake
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.