pango attributes with pygobject
Asked Answered
C

2

9

I have the following code, that uses pygtk:

attr = pango.AttrList()
attr.change(pango.AttrSize((
            50 * window_height / 100) * 1000, 0, -1))
attr.change(pango.AttrFamily("Sans", 0, -1))
attr.change(pango.AttrWeight(pango.WEIGHT_BOLD, 0, -1))
attr.change(pango.AttrForeground(65535, 65535, 65535, 0, -1))

self.label.set_attributes(attr)

I'm trying to port it to pygobject, but there is no class Pango.AttrFamily, neither Pango.AttrWeight, neither Pango.AttrForeground (and I can not instantiate a Pango.AttrSize).

The question is: how to use pango_attr_size_new, pango_attr_weight_new, pango_attr_family_new and pango_attr_foreground_new through instrospection?

I know I could use markup to do this, but 1. using attributes would keep things simpler and 2. I want to know what is happening here! I've already spent a lot of time trying to solve it.

Culver answered 9/1, 2012 at 3:39 Comment(3)
Did you get anywhere with this? I'm having the same problem.Incoordination
FIY, here's the relevant issue: bugzilla.gnome.org/show_bug.cgi?id=646788 (unchanged since 2014)Indorse
This has not been fixed yet. But you can workaround it by using the new CSS system: #mylabel {color:blabla; etc;}, set self.label.set_name("mylabel") and then importing the CSS file Gtk.CssProvider.load_from_file(Gtk.CssProvider_get_default(), file).Fatten
C
2

Edit: As the method override_font is deprecated you should use CSS as is described at this page - https://developer.gnome.org/gtk3/stable/GtkWidget.html#gtk-widget-override-font

The rest of the answer stays for history purposes.

I have no idea why that wouldn't work.

Here's a method to do it.

def set_global_styles(self, editor_widget):
    pango_context = editor_widget.create_pango_context()
    font_description = pango_context.get_font_description()
    increase = 14 #pt 14
    font_size = 1024*increase
    font_description.set_size(font_size)
    editor_widget.override_font(font_description)

So far the easiest way I found to do it. Late answer, but better late than never.

Just in case some resources for Pango and using the above code. I'm not sure if all the documentation for Pango applies to gtk3, but it worked for me.

Pango context set font description

Pango font description

Pango fonts in gtk

Pango layout

GtkWidget inherited by text editors among other objects.

Cw answered 20/8, 2012 at 6:2 Comment(1)
Do not use this code. override_font has been deprecated. developer.gnome.org/gtk3/stable/…Fatten
T
1

from this mail I found that this runs without runtime error:

    from gi.repository import Pango

    ren = Gtk.CellRendererText()
    ren.set_property('alignment', Pango.Alignment.RIGHT)

although in my code this did NOT make the content align to the right, but I might have another error in my code. Most important here is that the above code seems to be the translation for pygtk stuff like import pango and: pango.ALIGN_RIGHT

This is not the complete answer to OP, but hopefully one step nearer.

Tswana answered 6/3, 2016 at 2:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.