python 如何在 GTK 中更改字体大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1269326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 21:49:34  来源:igfitidea点击:

How can I change the font size in GTK?

pythonfontsgtkpygtkgtk2

提问by Claudiu

Is there an easy way to change the font size of text elements in GTK? Right now the best I can do is do set_markupon a label, with something silly like:

有没有一种简单的方法可以更改 GTK 中文本元素的字体大小?现在我能做的最好的事情就是set_markup在标签上做一些愚蠢的事情,比如:

lbl.set_markup("<span font_desc='Tahoma 5.4'>%s</span>" % text)

This 1) requires me to set the font , 2) seems like a lot of overhead (having to parse the markup), and 3) would make it annoying to change the font size of buttons and such. Is there a better way?

这 1) 需要我设置字体,2) 似乎有很多开销(必须解析标记),并且 3) 会使更改按钮等的字体大小变得烦人。有没有更好的办法?

回答by Andrew Y

If you want to change font overall in your app(s), I'd leave this job to gtkrc (then becomes a google question, and "gtkrc font" query brings us to this ubuntu forums linkwhich has the following snippet of the the gtkrc file):

如果您想在您的应用程序中整体更改字体,我会将这项工作留给 gtkrc(然后成为一个谷歌问题,“gtkrc 字体”查询将我们带到这个 ubuntu 论坛链接,其中包含以下代码片段gtkrc 文件):

style "font"
{
font_name = "Corbel 8"
}
widget_class "*" style "font"
gtk-font-name = "Corbel 8"

(replace the font with the one you/user need)

(用您/用户需要的字体替换字体)

Then the user will get consistent experience and will be able to change the settings easily without need for them to poke in the code and without you needing to handle the overhead of maintaining your personal configuration-related code. I understand you can make this setting more specific if you have a more precise definition for the widget_class.

然后用户将获得一致的体验,并且能够轻松更改设置,而无需他们插入代码,也无需处理维护个人配置相关代码的开销。我知道如果您对 widget_class 有更精确的定义,您可以使此设置更具体。

YMMV for different platforms, but AFAIK this file is always present at some location if GTK is being used, and allows to the user to be in charge of presentation details.

YMMV 适用于不同平台,但 AFAIK 如果使用 GTK,此文件始终存在于某个位置,并允许用户负责演示细节。

回答by Reed Copsey

In C, you can do:

在 C 中,您可以执行以下操作:

gtk_widget_modify_font(lbl, pango_font_description_from_string("Tahoma 5.4"));

In PyGTK, I believe it's something like:

在 PyGTK 中,我相信它类似于:

pangoFont = pango.FontDescription("Tahoma 5.4")
lbl.modify_font(pangoFont)