exupero exupero - 11 months ago 71
Python Question

How to Create a Multiline Button

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

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
, I haven't been able to find out how big the button is (it's packed in a table).

Any suggestions?


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 ()