pygobject的pango属性 [英] pango attributes with pygobject

查看:110
本文介绍了pygobject的pango属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下使用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_new 通过内省?

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

解决方案

:不推荐使用override_font方法时,您应按照本页所述使用CSS- Pango上下文集字体说明

Pango字体说明

gtk中的探戈字体

Pango布局

文本编辑器继承的GtkWidget 其他对象.

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.

解决方案

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.

这篇关于pygobject的pango属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆