我有一个GtkEntry,我想让用户根据自己选择的字体(或系统默认字体)来设置样式。我最后用一个Pango描述字符串来描述字体,比如"Monospace 10“。
我目前使用的是override_font,为了支持CSS样式,它已被弃用。
我至少想尝试“正确”,但现在从Pango字符串中获取CSS似乎是一个相当复杂和脆弱的工作流程。这是一个example from Github
def _get_editor_font_css():
"""Return CSS for custom editor font."""
font_desc = Pango.FontDescription("monospace")
if (gaupol.conf.editor.custom_font and
gaupol.conf.editor.use_custom_font):
font_desc = Pango.FontDescription(gaupol.conf.editor.custom_font)
# They broke theming again with GTK+ 3.22.
unit = "pt" if Gtk.check_version(3, 22, 0) is None else "px"
css = """
.gaupol-custom-font {{
font-family: {family},monospace;
font-size: {size}{unit};
font-weight: {weight};
}}""".format(
family=font_desc.get_family().split(",")[0],
size=int(round(font_desc.get_size() / Pango.SCALE)),
unit=unit,
weight=int(font_desc.get_weight()))
css = css.replace("font-size: 0{unit};".format(unit=unit), "")
css = css.replace("font-weight: 0;", "")
css = "\n".join(filter(lambda x: x.strip(), css.splitlines()))
return css在CSS在字符串中之后,我可以创建一个CSSProvider并将其传递给样式上下文的add_provider() (顺便说一下,这会不会导致累积CSS提供者?)。
这一切看起来都像是要做很多工作才能让字体回到系统中,在那里它可能会直接回到Pango中!
这真的是正确的方法吗?
https://stackoverflow.com/questions/41257101
复制相似问题