On 2011-07-18 04:13, Mauro Giacomini wrote:
Hi,
I have one question.
Is it possible to make a gtk.Window act as a gtk.Dialog?
I am in this situation:
In my app I have one gtk.Window (the main window); in this windows I
open a second Window where I collect input from the user.
I made this second window modal and set transient for parent (main
window), but I want that the flow in main window stops until I collect
the input in the second window.
I know gtk.Dialog acts as I want, but, if gtk.Dialog is pre-build with a
vbox and action area.
If I want to add widget, then I must wrote:
dialog.vbox.pack_start(widget).
The window from which I collect data is little complex, I have 2
notebooks, various treeview.
I hope that I explained well
Attached is a small class that runs a gtk.Window in the way that
gtk.Dialog.run does. It includes a few features that I hope make it easy
to use in real code. If you click Ok it'll print 'ok', or 'cancel' if
you click Cancel, or None if you close the window without clicking a button.
A few points to be careful of:
- Remember to show the window before calling RunAsDialog.run().
- Remember to explicitly destroy the window afterwards, after pulling
any information you need from the widgets within the window.
--
Tim Evans
Senior Software Engineer
ARANZ Geo Limited
p: +64 3 374 6120 | e: t.ev...@aranz.com
www.aranzgeo.com
from __future__ import division
import gtk, gobject
class RunAsDialog(object):
def __init__(self, window):
self.window = window
self.window.connect('delete-event', self.__on_delete_event)
self.__loop = None
self.__response = None
def __on_delete_event(self, window, event):
self.finish(None)
return True
def register_button(self, button, response):
def on_clicked(button):
self.finish(response)
button.connect('clicked', on_clicked)
def run(self):
self.__loop = gobject.MainLoop()
self.__loop.run()
return self.__response
def finish(self, response):
if self.__loop is not None:
self.__response = response
self.__loop.quit()
self.__loop = None
def test():
ok = gtk.Button(stock='gtk-ok')
cancel = gtk.Button(stock='gtk-cancel')
box = gtk.HButtonBox()
box.props.spacing = 6
box.pack_start(cancel)
box.pack_start(ok)
w = gtk.Window()
w.props.border_width = 12
w.add(box)
w.show_all()
runner = RunAsDialog(w)
runner.register_button(ok, 'ok')
runner.register_button(cancel, 'cancel')
print runner.run()
w.destroy()
if __name__ == '__main__':
test()
___
pygtk mailing list pygtk@daa.com.au
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/