views:

69

answers:

1

I'm trying to create a multiline button with PyGTK. I have a label added to my subclass of gtk.Button, but I'm having trouble sizing the label to the button. If the label makes it's own size, there is no text wrapping even with label.set_line_wrap(True) because the label simply resizes beyond the bounds of the button. I would set the size of the label to that of the button, but unless I explicitly set the size of the button using set_size_request, I haven't been able to find out how big the button is (it's packed in a table).

Any suggestions?

+1  A: 

In general, this is not possible with GTK+, because there is no stage where widgets "negotiate" sizes. Instead, widgets report their required size and after that container allocate some areas (normally, equal to or larger than required). In GTK+ 3 there will be width-for-height negotiation, so if your button (rather its label) is anyway going to be allocated several lines, it will be able to request less width and wrap its text.

In 2.x the best you can do is probably use width_chars property of gtk.Label:

import gtk

window = gtk.Window ()
align  = gtk.Alignment (0.5, 0.5)
button = gtk.Button ('a very long, possibly multiline text')

label = button.child
label.props.wrap = True
label.props.width_chars = 20

window.set_default_size (500, 500)
window.connect ('destroy', lambda *ignored: gtk.main_quit ())

window.add (align)
align.add (button)
window.show_all ()

gtk.main ()
doublep
Not only does that work, but it's much cleaner code than what I was putting together.
Colorado