[pygtk] Modal window and gtk.main()?

Christian Becke christianbecke at web.de
Fri Oct 23 05:13:26 WST 2009


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.


More information about the pygtk mailing list