gtk: label which goes multi-line instead of expanding horizontally
I have a VBox
which looks like this:
ImportantWidget
HSeparator
Label
I want this window to be only as wide as ImportantWidget
needs to be, and no wider. However, the Label
can sometimes grow to be very long. I want the following logic: if Label
can fit all its text without expanding the VBox
horizontally (after it has grown enough to fit ImportantWidget
), then its text should all be on one line. But if it would overflow and cause horizontal resizing, then it should instead split its text across multiple lines.
Is there a widget that does this 开发者_JAVA百科already, that's better than Label
for the task? If not, what should I use?
It looks like you want a dynamically resizing label, which GTK doesn't do out of the box. There's a Python port of VMWare's WrapLabel widget in the Meld repository. (From this question.)
Ah yes this shows how to do it:
l = gtk.Label("Painfully long text" * 30)
l.set_line_wrap(True)
EDIT:
example of a dynamic label who works in multi-line according to the size of the window and text:
import gtk
class DynamicLabel(gtk.Window):
def __init__(self):
gtk.Window.__init__(self)
self.set_title("Dynamic Label")
self.set_size_request(1, 1)
self.set_default_size(300,300)
self.set_position(gtk.WIN_POS_CENTER)
l = gtk.Label("Painfully long text " * 30)
l.set_line_wrap(True)
l.connect("size-allocate", self.size_request)
ImportantWidget = gtk.Label("ImportantWidget")
vbox = gtk.VBox(False, 2)
HSeparator = gtk.HSeparator()
vbox.pack_start(ImportantWidget, False, False, 0)
vbox.pack_start(HSeparator, False, False, 0)
vbox.pack_start(l, False, False, 0)
self.add(vbox)
self.connect("destroy", gtk.main_quit)
self.show_all()
def size_request(self, l, s ):
l.set_size_request(s.width -1, -1)
DynamicLabel()
gtk.main()
精彩评论