9

我有以下代码,它使用 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)

我正在尝试将其移植到 pygobject,但没有类 Pango.AttrFamily、Pango.AttrWeight 和 Pango.AttrForeground(我无法实例化 Pango.AttrSize)。

问题是:如何使用pango_attr_size_new, pango_attr_weight_new,pango_attr_family_newpango_attr_foreground_new通过自省?

我知道我可以使用标记来做到这一点,但是 1. 使用属性会使事情变得更简单 2. 我想知道这里发生了什么!我已经花了很多时间试图解决它。

4

2 回答 2

2

编辑:由于该方法override_font已被弃用,您应该使用本页所述的 CSS - https://developer.gnome.org/gtk3/stable/GtkWidget.html#gtk-widget-override-font

其余的答案保留用于历史目的。

我不知道为什么那行不通。

这是一种方法。

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)

到目前为止,我找到了最简单的方法。迟到的答案,但迟到总比没有好。

以防万一 Pango 的一些资源和使用上面的代码。我不确定 Pango 的所有文档是否都适用于 gtk3,但它对我有用。

Pango 上下文设置字体描述

Pango字体说明

gtk 中的 Pango 字体

盘古布局

GtkWidget在其他对象中由文本编辑器继承。

于 2012-08-20T06:02:07.593 回答
0

从这封邮件中我发现它运行时没有运行时错误:

    from gi.repository import Pango

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

尽管在我的代码中这并没有使内容与右侧对齐,但我的代码中可能还有另一个错误。这里最重要的是,上面的代码似乎是 pygtk 内容的翻译,例如import pangopango.ALIGN_RIGHT

这不是对 OP 的完整答案,但希望更近一步。

于 2016-03-06T02:21:45.590 回答