Thanks a lot for your simple example Christian. It works like a charm
and it also made it easy to reproduce my issue. Take a look at the
additions I made to your code. I added an idle hook to each of the
windows and the code now behave like my application (show the second
window and you can't use the button to close the first one anymore,
you can kill it by closing it by the window manager but it will leave
the interpreter hanging). So what have I done wrong? Setting idle
hooks in combination with nested gtk.main() or is it because i call
gtk.events_pending() and gtk.main_iteration(False) in the idle hooks?
I don't know if I am supposed to be able to call gtk.events_pending()
or gtk.main_iteration(False) in an idle hook but it worked until I
started to use a nested gtk.main()...
Example:
-----------------------8<-----------------------
import gtk, gobject
class SecondWin:
def __init__ (self, parent):
self.parent = parent
self.w = gtk.Window ()
# Disable interaction with parent
self.w.set_modal (True)
# Tell WM this is a dialog
self.w.set_type_hint (gtk.gdk.WINDOW_TYPE_HINT_DIALOG)
# Tell WM this window belongs to parent
self.w.set_transient_for (parent)
self.w.connect ("delete-event", self.hide)
box = gtk.VButtonBox ()
self.w.add (box)
b = gtk.Button (stock=gtk.STOCK_CLOSE)
b.connect ("clicked", self.hide)
box.add (b)
gobject.idle_add(self.on_idle)
def show (self):
self.w.show_all ()
# Indicate visually that interaction with parent
# is not possible while we are shown
self.parent.set_sensitive (False)
def on_idle(self):
while gtk.events_pending():
gtk.main_iteration(False)
return True
def hide (self, *args):
# Indicate visually that interaction with parent
# is possible again
self.parent.set_sensitive (True)
self.w.hide ()
return True
class MainWin:
def __init__ (self):
self.w = gtk.Window ()
self.w.set_position (gtk.WIN_POS_CENTER)
self.w.connect ("delete-event", gtk.main_quit)
box = gtk.VButtonBox ()
self.w.add (box)
self.sw = SecondWin (self.w)
b = gtk.Button ("show second win")
b.connect ("clicked", self.show_second_win, self.sw)
box.pack_start (b, False)
b = gtk.Button (stock=gtk.STOCK_QUIT)
b.connect ("clicked", gtk.main_quit)
box.pack_start (b, False)
gobject.idle_add(self.on_idle)
def on_idle(self):
while gtk.events_pending():
gtk.main_iteration(False)
return True
def run (self):
self.w.show_all ()
gtk.main ()
def show_second_win (self, button, w):
w.show ()
w = MainWin ()
w.run ()
-----------------------8<-----------------------
On Thu, Oct 22, 2009 at 5:13 PM, Christian Becke <[email protected]>wrote:
> Robert Palmqvist schrieb:
> > I wanted to take a look at the source for gtk.Dialog to se if I could
> > mimic the “run” method from a gtk.Window but I never found the actual
> > source for gtk.Dialog.run().
>
> The source is here:
> http://git.gnome.org/cgit/gtk+/tree/gtk/gtkdialog.c#n993
>
> > Where can I find examples of best practise to implement and handle more
> > than one window (more than one gtk.Window, not the use of one
> > gtk.Window and additional windows realized with the use of gtk.Dialog or
> > its sub-classes) in a pygtk application (I can’t find any examples in
> > the documentation, the tutorial or the FAQ, am I missing something
> > obvious here?)?
>
> Example:
> -----------------------8<-----------------------
> import gtk
>
> class SecondWin:
>
> def __init__ (self, parent):
> self.parent = parent
> self.w = gtk.Window ()
>
> # Disable interaction with parent
> self.w.set_modal (True)
>
> # Tell WM this is a dialog
> self.w.set_type_hint (gtk.gdk.WINDOW_TYPE_HINT_DIALOG)
>
> # Tell WM this window belongs to parent
> self.w.set_transient_for (parent)
> self.w.connect ("delete-event", self.hide)
>
> box = gtk.VButtonBox ()
> self.w.add (box)
>
> b = gtk.Button (stock=gtk.STOCK_CLOSE)
> b.connect ("clicked", self.hide)
> box.add (b)
>
>
> def show (self):
> self.w.show_all ()
> # Indicate visually that interaction with parent
> # is not possible while we are shown
> self.parent.set_sensitive (False)
>
>
> def hide (self, *args):
> # Indicate visually that interaction with parent
> # is possible again
> self.parent.set_sensitive (True)
> self.w.hide ()
> return True
>
>
> class MainWin:
>
> def __init__ (self):
> self.w = gtk.Window ()
> self.w.set_position (gtk.WIN_POS_CENTER)
> self.w.connect ("delete-event", gtk.main_quit)
>
> box = gtk.VButtonBox ()
> self.w.add (box)
>
> self.sw = SecondWin (self.w)
>
> b = gtk.Button ("show second win")
> b.connect ("clicked", self.show_second_win, self.sw)
> box.pack_start (b, False)
>
> b = gtk.Button (stock=gtk.STOCK_QUIT)
> b.connect ("clicked", gtk.main_quit)
> box.pack_start (b, False)
>
>
> def run (self):
> self.w.show_all ()
> gtk.main ()
>
>
> def show_second_win (self, button, w):
> w.show ()
>
>
> w = MainWin ()
> w.run ()
> ----------------------->8-----------------------
>
> HTH.
> _______________________________________________
> pygtk mailing list [email protected]
> http://www.daa.com.au/mailman/listinfo/pygtk
> Read the PyGTK FAQ: http://faq.pygtk.org/
>
_______________________________________________
pygtk mailing list [email protected]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/