如何风格的CSS GtkLabel的? [英] How to style a GtkLabel with CSS?
问题描述
我试图使用CSS样式GtkLabel的。我想更改标签的颜色和字体大小。这里是我的C code:
I'm trying to use CSS to style a GtkLabel. I would like to change the color and font size of the label. Here is my C code:
#include <gtk/gtk.h>
int main(int argc, char *argv[]) {
gtk_init(&argc, &argv);
GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
GtkWidget *label = gtk_label_new("Label");
GtkCssProvider *cssProvider = gtk_css_provider_new();
gtk_css_provider_load_from_path(cssProvider, "theme.css", NULL);
gtk_style_context_add_provider(gtk_widget_get_style_context(window),
GTK_STYLE_PROVIDER(cssProvider),
GTK_STYLE_PROVIDER_PRIORITY_USER);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_container_add(GTK_CONTAINER(window), label);
gtk_widget_show_all(window);
gtk_main();
}
下面是我在CSS theme.css
,目前这只能改变字体颜色:
Here is my CSS in theme.css
, currently this should only change the font color:
GtkLabel {
color: green;
}
我编译:
gcc `pkg-config gtk+-3.0 --cflags` test.c -o test `pkg-config --libs gtk+-3.0`
然而,当我运行code,我得到的黑白文本。如果我改变 theme.css
来使用窗口小部件名称或CSS类,而不是 GtkLabel的
,它仍然无法正常工作。不过,如果我使用 *
而不是 GtkLabel的
,它的工作原理(虽然我不希望这适用于一切)。这意味着C code必须是正确的,什么是错的CSS。
However, when I run the code I get black text. If I change theme.css
to use a widget name or CSS class instead of GtkLabel
, it still doesn't work. However, if I use *
instead of GtkLabel
, it works (although I don't want this to apply to everything). This means that the C code should be correct and something is wrong with the CSS.
推荐答案
目前,CSS提供者没有遗传到孩子样式上下文。因此,你需要使用CSS提供商添加到屏幕 gtk_style_context_add_provider_for_screen()
Currently, CSS providers are not inherited to the children style contexts. So you need to add the CSS provider to the screen using gtk_style_context_add_provider_for_screen()
尝试更改
gtk_style_context_add_provider(gtk_widget_get_style_context(窗口),
GTK_STYLE_PROVIDER(cssProvider)
GTK_STYLE_PROVIDER_PRIORITY_USER);
到
gtk_style_context_add_provider_for_screen(gdk_screen_get_default(),
GTK_STYLE_PROVIDER(cssProvider)
GTK_STYLE_PROVIDER_PRIORITY_USER);
我不认为GTK支持多个屏幕的这些天,但 gtk_widget_get_screen()
可以用来代替 gdk_screen_get_default()
。
I don't think gtk supports multiple screens these days, but gtk_widget_get_screen()
could be used to replace gdk_screen_get_default()
.
这篇关于如何风格的CSS GtkLabel的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!