pyGTK -- how to wait for modal dialog?

Steffen Ries steffen.ries at sympatico.ca
Thu Oct 12 08:13:36 EDT 2000


ge at nowhere.none (Grant Edwards) writes:

> In article <nK%E5.245$FU3.30895 at ptah.visi.com>, Grant Edwards wrote:
> 
> >I've created a modal dialog box, but can't figure out how to
> >wait until the dialog is dismissed so I can find out which
> >button was pressed.
> 
> Here's what I've figured out so far (it seems to work), is it
> the recommended idiom?
> 
> class FooBar(GtkDialog):
>     __super = GtkDialog
>     def __init__(self,msg,buttonTextList):
>         self.__super.__init__(self)
>         self.__returnString = None
>         self.set_modal(TRUE)
>         [...]
>         
>     def clicked(self,buttonWidget,buttonText):
>         self.__returnString = buttonText
>         self.destroy()
>     
>     def wait(self):
>         while self.__returnString is None:
>             mainitereation()
>         return self.__returnString
> 
> The important thing is that mainiteration blocks until it
> receives an event -- this is the default behavior.


I came up with a similar solution, but I can't say whether *that* one
is recommended:

class Dialog(GtkDialog):
  def __init__(self, parent, ...):
    ...
    self.retVal = None
    self.set_modal(TRUE)
    self.set_transient_for(parent)

  def run(self):
    self.show_all()
    self.dest_id = self.connect("destroy", self.close)
    mainloop()         # blocks until mainquit() is called
    return self.retVal

  def close(self, _o, val=None):
    self.retVal = val
    self.disconnect(self.dest_id)
    self.destroy()
    mainquit()

The actual dialogs would inherit from Dialog and connect self.close()
to some button, which passes the appropriate return value. (Just
closing the window acts like canceling the dialog).

hth,
/steffen
-- 
steffen.ries at sympatico.ca	<> Gravity is a myth -- the Earth sucks!



More information about the Python-list mailing list