GTK +设置字体大小 [英] GTK+ Set Font Size

查看:345
本文介绍了GTK +设置字体大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之前

对于我的用例而言,MenuItems太小了.
因此,我的问题是,如何增加文本保存",加载"和退出"的字体大小?""

the MenuItems are too small for my use case.
Therefore, my question is, "how I may increase the font size of the text "Save", "Load" and "Exit?" "

下面的代码能够更改保存"文本的样式,除了无法更改字体大小.

The code below is able to change the style of the "Save" text, except that it is not able to change the font-size.

执行可执行文件时,会出现以下警告:

When the executable is executed, the following warning comes up:

Gtk-WARNING **: Theme parsing error: Styles.css:10:14: Junk at end of value


font-size: 25px;行被删除(或注释掉)后,警告消失.


When the line font-size: 25px; is removed (or commented out), the warning disappears.

看来font-size在GTK + 3中未实现,但在
处被列为有效属性. https://developer.gnome. org/gtk3/stable/chap-css-properties.html#id-1.5.3.3.15

This may seem like font-size is not implemented in GTK+3, but it is listed as a valid property at
https://developer.gnome.org/gtk3/stable/chap-css-properties.html#id-1.5.3.3.15


所以问题仍然存在,"如何使用这种方法增加字体大小?"

So the question remains, "how do I increase the font size using this method?"


Styles.css

Styles.css

/*
    Valid Font Properties:
        https://developer.gnome.org/gtk3/stable/chap-css-properties.html#id-1.5.3.3.15
*/

.Item_Save {
    background: rgba(000, 255, 000, 1);
    font-style: italic;
    font-weight: 800;
    font-size: 25px;
    padding: 10px 10px 10px 10px;
}


C源文件

#include <gtk/gtk.h>

int main (int argc, char *argv[]) {
    gtk_init(&argc, &argv);


    GtkCssProvider* Provider = gtk_css_provider_new();
    GdkDisplay* Display = gdk_display_get_default();
    GdkScreen* Screen = gdk_display_get_default_screen(Display);

    gtk_style_context_add_provider_for_screen(Screen, GTK_STYLE_PROVIDER(Provider), GTK_STYLE_PROVIDER_PRIORITY_USER);
    gtk_css_provider_load_from_path(GTK_CSS_PROVIDER(Provider), "Styles.css", NULL);


    GtkWidget* Window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

    GtkWidget* MenuBar = gtk_menu_bar_new();
    GtkWidget* MenuItem_File = gtk_menu_item_new_with_mnemonic("_File");

    GtkWidget* SubMenu1 = gtk_menu_new();

    GtkWidget* Item_Save = gtk_menu_item_new_with_mnemonic("_Save");
    GtkWidget* Item_Load = gtk_menu_item_new_with_mnemonic("_Load");
    GtkWidget* Item_Exit = gtk_menu_item_new_with_mnemonic("_Exit");



    GtkStyleContext *Context;
    Context = gtk_widget_get_style_context(Item_Save);
    gtk_style_context_add_class(Context, "Item_Save");



    gtk_menu_shell_append(GTK_MENU_SHELL(SubMenu1), Item_Save);
    gtk_menu_shell_append(GTK_MENU_SHELL(SubMenu1), Item_Load);

    gtk_menu_shell_append(GTK_MENU_SHELL(SubMenu1), gtk_separator_menu_item_new());

    gtk_menu_shell_append(GTK_MENU_SHELL(SubMenu1), Item_Exit);


    gtk_menu_item_set_submenu(GTK_MENU_ITEM(MenuItem_File), SubMenu1);
    gtk_menu_shell_append(GTK_MENU_SHELL(MenuBar), MenuItem_File);


    GtkWidget* VerticalBox;

    VerticalBox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);

    gtk_box_pack_start(GTK_BOX(VerticalBox), MenuBar, false, false, 0);
    gtk_container_add(GTK_CONTAINER(Window), VerticalBox);


    gtk_widget_show_all(MenuBar);
    gtk_widget_show(VerticalBox);


    gtk_window_set_default_size(GTK_WINDOW(Window), 950, 600);
    gtk_window_set_position(GTK_WINDOW(Window), GTK_WIN_POS_CENTER);
    gtk_window_set_title(GTK_WINDOW(Window), "My Title");

    gtk_widget_show(Window);


    gtk_main();

    return 0;
}

推荐答案

您可能正在点击这个错误.似乎某些版本的Gtk3忽略了font-size和font-face.该错误是在Ubuntu上报告的,但是我找不到它是否已转发给Gtk3团队.

You might be hitting this bug. It seems that some versions of Gtk3 ignore the font-size and the font-face. This bug was reported at Ubuntu, but I can't find if this was forwarded to the Gtk3 team.

另一方面,Gtk错误跟踪器报告了Gtk.3.16中与相关的问题+ ,这可能与您的问题有关.

On the other hand, the Gtk bug tracker reports a related problem in Gtk.3.16+, which might be related to your issue.

我将在此处尝试编译您的程序并进行报告.

I'll try to compile your program here and report back.

报告:

在这里似乎一切正常:

请注意,我已经安装了Gtk +版本3.20.6.另外,请注意,与窗口标题相比,原始字体大小看起来相当合理(文件",加载"和退出").

Note that I have Gtk+ version 3.20.6 installed. Also, note that the original font sizes look quite reasonable ('File', 'Load', and 'Exit') when compared to the window title.

更新:根据您的评论,我认为您仍在使用3.6.x版本.自3.6以来就有很大的不同!特别是CSS实现已经明显成熟.从3.18到3.20,许多事情发生了变化.我很确定自3.6版以来字体大小问题已经解决. (刚刚检查-3.6已使用3年了). CSS系统变化的一个指标是,有必要在每个新的Gtk +版本中更新所有Gtk +主题".

UPDATE: From your comment, I gather you are still on version 3.6.x. There are huge differences since 3.6! Particularly the CSS implementation has matured noticeably. Just from 3.18 to 3.20, many, many things changed. I'm quite sure the font size issue has been solved since 3.6. (Just checked - 3.6 is almost 3 years old). One indicator for the changes of the CSS system, is that it was necessary to update all Gtk+ 'themes' in each new Gtk+ version.

这篇关于GTK +设置字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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