[pygtk] how to keep toplevel window shrink wrapped around contents

Karl Ostmo kostmo at gmail.com
Sun Sep 7 12:11:33 WST 2008


Hi,
Please excuse the number of postings coming from me - I'm using my weekend
time to write up a number of questions I've had for quite a while.

Often in my applications I have widgets that grow or shrink upon user
interaction.  It can be a bit unsightly, however, after large widgets are
collapsed, leaving vast empty spaces in the window.

Is there a way to keep the toplevel window permanently at its minimum size,
respecting the allocation requests of its children but no larger?  I've
tried resizing the window to (1, 1) inside every callback for widget
shrinking operations, but sometimes even that doesn't work, as demonstrated
in my code below.

If there is no easy way to do this now, I think it would make a good feature
for the next GTK version.

Karl

#!/usr/bin/python

from pygtk import require
require('2.0')
import gtk

class Shrinkable(gtk.Window):

    def __init__(self):
        gtk.Window.__init__( self, gtk.WINDOW_TOPLEVEL )
        vbox = gtk.VBox(False)
        self.add(vbox)

        button = gtk.Button("Shrink wrap")
        button.connect("clicked", self.shrinkwrap_window)
        vbox.pack_start(button, False, False)

        button = gtk.Button("Dummy button")
        button.set_size_request(200, 200)

        my_expander = gtk.Expander("Expander")
        my_expander.add(button)
#        my_expander.connect("activate", self.cb_expansion)
        my_expander.connect("notify::expanded", self.cb_expansion)

        vbox.pack_start(my_expander, False, False)
        self.show_all()

    def shrinkwrap_window(self, widget):
        self.resize(1, 1)

    def cb_expansion(self, expander, param_spec=None, user_data=None):
        if expander.get_expanded():
            print "expanded."
        else:
            print "collapsed."
            self.shrinkwrap_window(expander)

if __name__ == "__main__":
    application = Shrinkable()
    gtk.main()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.daa.com.au/pipermail/pygtk/attachments/20080907/c39ba4b0/attachment.htm 


More information about the pygtk mailing list