How to repeatedly show a Dialog with PyGTK / Gtkbuilder?
Posted
by
Julian
on Stack Overflow
See other posts from Stack Overflow
or by Julian
Published on 2011-01-11T11:56:54Z
Indexed on
2011/01/11
22:53 UTC
Read the original article
Hit count: 251
I have created a PyGTK application that shows a Dialog when the user presses a button.
The dialog is loaded in my __init__
method with:
builder = gtk.Builder()
builder.add_from_file("filename")
builder.connect_signals(self)
self.myDialog = builder.get_object("dialog_name")
In the event handler, the dialog is shown with the command self.myDialog.run()
, but this only works once, because after run()
the dialog is automatically destroyed. If I click the button a second time, the application crashes.
I read that there is a way to use show()
instead of run()
where the dialog is not destroyed, but I feel like this is not the right way for me because I would like the dialog to behave modally and to return control to the code only after the user has closed it.
Is there a simple way to repeatedly show a dialog using the run()
method using gtkbuilder? I tried reloading the whole dialog using the gtkbuilder, but that did not really seem to work, the dialog was missing all child elements (and I would prefer to have to use the builder only once, at the beginning of the program).
[SOLUTION]
As pointed out by the answer below, using hide()
does the trick. But one has to take care that the dialog is in fact destroyed if one does not catch its "delete-event". A simple example that works is:
import pygtk
import gtk
class DialogTest:
def rundialog(self, widget, data=None):
self.dia.show_all()
result = self.dia.run()
def destroy(self, widget, data=None):
gtk.main_quit()
def closedialog(self, widget, data=None):
self.dia.hide()
return True
def __init__(self):
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.connect("destroy", self.destroy)
self.dia = gtk.Dialog('TEST DIALOG', self.window,
gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT)
self.dia.vbox.pack_start(gtk.Label('This is just a Test'))
self.dia.connect("delete-event", self.closedialog)
self.button = gtk.Button("Run Dialog")
self.button.connect("clicked", self.rundialog, None)
self.window.add(self.button)
self.button.show()
self.window.show()
if __name__ == "__main__":
testApp = DialogTest()
gtk.main()
© Stack Overflow or respective owner