Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python GTK3 limit label width

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()
like image 821
doxin Avatar asked Feb 16 '15 23:02

doxin


2 Answers

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.

like image 155
elya5 Avatar answered Nov 15 '22 09:11

elya5


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.

like image 29
andlabs Avatar answered Nov 15 '22 09:11

andlabs