How to automatically resize text according to fixed size PyGTK shortcut?

I have a fixed size gtk.Label. I would like to automatically resize the text so that it matches the label. Is there an elegant way to do this with fonts that don't have a fixed width?

my_label = gtk.Label()

# Short text segment
my_label.set_text( "Short text segment." )

# Long text segment
### Determine required text size here. ###
my_label.set_text( "This is a really long text segment that I would like to resize."
+3
source share
1 answer

I did not have time to check this out, but something in this direction should do what you want:

size = 12000 # thousandths of a point
temp_label = gtk.Label(my_label.get_text())
while temp_label.get_width() > my_label.get_width():
    size -= 100
    temp_label.set_attributes(pango.Attrlist().insert(pango.AttrSize(size))
my_label = temp_label

This assumes you directly force the width of my_label. If my_label gets the width from something else (like the parent container), replace my_label.get_width () with whatever maximum width you want.

, 1/10 , . - 100 ( , ).

, , .

0

Source: https://habr.com/ru/post/1785833/


All Articles