gratz
gratz

Reputation: 1616

Resize Gtk Dialog after hiding/removing widgets

How do I get the modal dialog window to resize (shrink) to fit the size of it's children when child widgets are removed? The following cut down example sets the width of the modal dialog, adds the widgets (which expands the height automatically), however when removing a widget the remaining child widgets shift up but the modal stays the same height with empty space at the bottom.

import gtk
import pango

dialog = gtk.Dialog()
dialog.set_modal(True)

screen_width = gtk.gdk.screen_width()
screen_height = gtk.gdk.screen_height()

dialog.set_property('width-request', screen_width/3)
dialog.set_position(gtk.WIN_POS_CENTER_ON_PARENT)
dialog.action_area.set_layout(gtk.BUTTONBOX_SPREAD)


flash = gtk.Label("Foo")
flash.set_alignment(0, 0)
flash.set_line_wrap(True)
flash.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse('#a94442'))
flash.modify_font(pango.FontDescription("sans 48"))

eventbox = gtk.EventBox()
eventbox.add(flash)
eventbox.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse('#ebccd1'))

label = gtk.Label("Username:")

def check(widget=None):
    print "test"
    flash.hide()

entry = gtk.Entry()

button = gtk.Button('Check')
button.connect('clicked', check)

table = gtk.Table(2, 2, False)
table.attach(label, 0, 1, 0, 1, xoptions=gtk.SHRINK)
table.attach(entry, 1, 2, 0, 1, xoptions=gtk.EXPAND|gtk.FILL)
table.attach(button, 1, 2, 1, 2, xoptions=gtk.EXPAND|gtk.FILL)
table.show()

vbox = gtk.VBox()
vbox.pack_start(eventbox, False, False, 10)
vbox.pack_start(table, False, False, 10)

dialog.vbox.add(vbox)
dialog.show_all()
dialog.run()

Upvotes: 2

Views: 1793

Answers (1)

theGtknerd
theGtknerd

Reputation: 3745

Have you tried:

dialog.resize(400, 400)

The first integer is the width, the second is the height. You will need to play with the geometry to figure out what you need or then get the size of your widgets somehow.

P.S. This worked for me using Gtk 3.12. You did not post what your version is.

Upvotes: 2

Related Questions