python GTK3 limit label width

gtk, gtk3, label, python, width

Solution

So here is a solution (still not the exact pixel width) You can use Gtk.Widget.set_halign to force no horizontal expansion.

Here is the part of the code:

    lbl=gtk.Label(label)
    lbl.set_max_width_chars(5)
    lbl.set_ellipsize(pango.EllipsizeMode.END)
    lbl.set_halign(gtk.Align.CENTER)
    self.add(lbl)

This is what it looks like:

I hope I did not miss anything this time.

Problem

I've got a set of labels in a flowbox, the problem is that I would like for these labels to be 96px wide at most. I've set label.set_ellipsize(True), but since the flowbox gives them as much room as they like they don't get ellipsized, even though I've set their size request to 96px wide. I've tried every function I could find that seemed even tangentially related on all the widgets involved, but nothing seems to work. the only workaround I did find was using set_min_children_per_line() but that requires calculating the number of children from the flowbox width which is dependent on the number of children per row, leading to a flowbox that gets really wide real quick. I'm probably missing something obvious, but I've been bashing my head on this problem for days now. I've made this testcase that exhibits the problem when amount of columns isn't divisible by two: ``` from gi.repository import Gtk as gtk from gi.repository import Pango as pango class Widget(gtk.VBox): def __init__(self,label): gtk.VBox.__init__(self) image=gtk.Image.new_from_icon_name("image-missing",gtk.IconSize.DIALOG) image.set_size_request(96,96) self.add(image) lbl=gtk.Label(label) self.add(lbl) class TestCase(gtk.Window): def __init__(self): gtk.Window.__init__(self) lbl=gtk.Label("some text") scrollbox=gtk.ScrolledWindow() self.add(scrollbox) flowbox=gtk.FlowBox() scrollbox.add(flowbox) for i in range(50): w=Widget("longlabel"*5) flowbox.add(w) w=Widget("short") flowbox.add(w) if __name__=="__main__": w=TestCase() w.connect("delete-event",gtk.main_quit) w.show_all() gtk.main() ```

Original source