GTK +中的CSS样式 [英] CSS Styling in GTK+

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

问题描述

好吧,我最近开始在C语言中使用GTK +进行编程.我的代码中有一个GdkToggleButton,我想用CSS添加样式.我已经在这里 https://developer.gnome.org/gtk3/stable 阅读了几乎所有的GTK3 +文档.一个>,但没有找到适合我的情况.实际上,有关CSS的文档中有一部分,但是我找不到真正解析CSS文件的那一部分.

Ok, i recently started programming with GTK+ in C. I have a GdkToggleButton in my code and i want to add styling to it with css. I've read almost all of GTK3+ documentation here https://developer.gnome.org/gtk3/stable but found nothing that fits my case. Actually there is a part of the documentation about CSS but i couldn't find the part to actually parse the css file.

我要做的就是使用CSS或任何其他方法更改GdkToggleButton的颜色.

我想知道将css文件放在哪里以及如何解析它并将其添加到小部件中.

I want to know where to put the css file and how to parse it and add it to a Widget.

我实际上可以获得按钮的GdkRGBA(颜色),但是更改没有效果.这是我更改按钮GdkRGBA的方法.

I actually could get GdkRGBA (color) of a button but changes have no effect. Here's how i change GdkRGBA of button.

GdkRGBA *color ;
GtkWidget *button;
GtkStyleContext *context;

color = gdk_rgba_copy (color); // I don't know to initialize GdkRGBA :(

context = gtk_widget_get_style_context(button);    
button = gtk_toggle_button_new_with_label ("Hello World");
gtk_style_context_get_color(context,
                            GTK_STATE_FLAG_NORMAL,
                            color);
gdk_rgba_parse(color,
                "red");

已解决

SOLVED

gtk_css_provider_load_from_path (cssProvider,
                                    "/home/shadow_m2/code.css",
                                    NULL);
gtk_style_context_add_provider (context,
                                    GTK_STYLE_PROVIDER(cssProvider),
                                    GTK_STYLE_PROVIDER_PRIORITY_USER); // I had used wrong priority on first try
gtk_style_context_save (context);

推荐答案

您可以使用 gtk_css_provider_load_from_data()加载程序中嵌入的自定义CSS.GtkCssProvider通常是加载CSS的方法.

You can load custom CSS embedded in your program with gtk_css_provider_load_from_data(). GtkCssProvider is how to load CSS in general.

然后,您将获取GtkWidget的样式上下文,并在其上调用 add_provider().使用 APPLICATION 优先级;无论如何,这就是您要添加的内容(特定于应用程序的CSS).

Then you get the GtkWidget's style context and call add_provider() on it. Use the APPLICATION priority; that's what you're adding anyway (application-specific CSS).

也不需要 save()调用.

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

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