12

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.

problem example

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()
doxin
  • 698
  • 1
  • 7
  • 22
  • 1
    Have you tried [`Gtk.Label.set_max_width_chars(n_chars)`](https://lazka.github.io/pgi-docs/Gtk-3.0/classes/Label.html#Gtk.Label.set_max_width_chars)? – elya5 Feb 18 '15 at 09:09
  • I have tried that. it doesn't change a thing, and it isn't what I want in any case. I want to set the width in pixels so it matches up with the icons. – doxin Feb 18 '15 at 14:24
  • 1
    Can you provide some code? Maybe there's some other property getting in the way (either by default or set by you elsewhere)... playing with GtkLabels is always fun x.x – andlabs Feb 18 '15 at 21:46
  • Not the actual answer for your question but `set_max_width_chars(n_chars)` works if you use it in combination with `set_line_wrap(True)` and `set_line_wrap_mode(pango.WrapMode.WORD_CHAR)` or whatever WrapMode you want. A solution with the exact pixel width probably has to enable WrapMode as well. – elya5 Feb 19 '15 at 08:20
  • if you'd have read the comments you'd have seen I already tried that. a) it doesn't work, and b) even if it did it doesn't do what I want in two ways. I want ellipses not line wraps. – doxin Feb 19 '15 at 14:05

2 Answers2

7

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: screeshot of the window

I hope I did not miss anything this time.

elya5
  • 2,236
  • 1
  • 11
  • 27
  • Alignments are depreciated. I'd rather not write a new program with depreciated features. it's also still not pixel width, so it isn't exactly solving my problem either. – doxin Feb 21 '15 at 15:26
3

You do have another option: use a custom GtkContainer implementation that limits its children to exact pixel widths. According to Erick Pérez Castellanos on irc.gimp.net/#gtk+, GNOME Contacts has one; here it is. It's in Vala, but it shouldn't be too hard to wrap your head around alongside some reading of the GtkWidget and GtkContainer (and GtkBin) semantics. Unfortunately I do not know how to create new GObjects in Python, sorry.

If you do use the approach in GNOME Contacts, the custom container (which derives from GtkBin, a convenience for single-child GtkContainers) will hold just your GtkLabel, and you add that to the flowbox item's GtkVBox.

Hopefully that helps.

andlabs
  • 11,290
  • 1
  • 31
  • 52