GTK+ Set Font Size

2019-07-27 06:07发布

As before,

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


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

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

/*
    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 Source File

#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;
}

1条回答
放我归山
2楼-- · 2019-07-27 07:10

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.

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.

Report:

It seems to be working just fine here:

enter image description here

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.

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.

查看更多
登录 后发表回答