Is there a simple way to change the value-text's font, align and color of a GtkScale widget? For example, I want to have a red and bold number, instead of the standard black one.
1 Answer
Ideally you'd set these properties in the theme. But if you need your application to override the theme, you can call gtk_widget_modify_font to make the text bold and gtk_widget_modify_fg to make it red. Using PyGTK for demonstration:
import gtk, pango
w = gtk.HScale()
# bold: get existing widget's font description and set weight to bold
fdesc = w.get_pango_context().get_font_description()
fdesc.set_weight(pango.WEIGHT_BOLD)
w.modify_font(fdesc)
# red
w.modify_fg(gtk.STATE_NORMAL, gtk.gdk.Color('red'))
# show it
win = gtk.Window()
win.add(w)
win.show_all()
gtk.main()
...results in:

5 Comments
Genesis Rock
That works great, but when I set
value_pos to GTK_POS_RIGHT, I have got an ugly spacing on the right, because GTK allocates (sometimes unused) space for high values. So I want to set the value-text's alignment. Is there any way to do so?user4815162342
@GenesisRock I'm not sure what you mean; when I set the spacing in my example with
w.set_value_pos(gtk.POS_RIGHT), I don't see the ugly spacing, or I'm unable to recognize it. Have you tried changing the value-spacing property of the widget?Genesis Rock
No, that's no what I meant. But in fact it's quite simple: GTK allocates enough space for the value label to display the whole range. But when the range is very high the allocated space is also very big and a huge gap is generated. The following image should explain this (range 0 to 1,000,000): !Screen. But that looks very ugly in my application and so I want to right-align the label. It should be like "SLIDER-SPACE-LABEL" instead of "SLIDER-LABEL-SPACE"
user4815162342
@GenesisRock There's
gtk_scale_get_layout(), but I am unable to produce a visible result by mutating the returned object. If I were you, I'd call gtk_scale_set_draw_value(False), and print the value myself. That would give you complete control over alignment and everything else.Genesis Rock
Yeah, I tired the layout.stuff for myself, too, and it's not really working. So I think it will be the best to display the value manually.