在此GTK+ change font to spin button和此how to apply CSS to GTK+ code?问题之后,我开始了解如何将css样式应用于GTK代码,但仍然存在许多令人困惑的细节。
这是我创建两个按钮和三个标签的代码:
test.c
#include <gtk/gtk.h>
#include <string.h>
static void
activate (GtkApplication *app,
gpointer user_data)
{
GtkWidget *window = gtk_application_window_new (app);;
GtkWidget *button_01 = gtk_button_new_with_label("This is button 01");
GtkWidget *button_02 = gtk_button_new_with_label("This is button 02");
GtkWidget *label0 = gtk_label_new("hello 0");
GtkWidget *label1 = gtk_label_new("hello 1");
GtkWidget *label2 = gtk_label_new("hello 2");
GtkWidget * main_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 25);
GtkCssProvider *cssProvider = gtk_css_provider_new();
gtk_css_provider_load_from_path(cssProvider, "theme.css", NULL);
gtk_style_context_add_provider_for_screen(gdk_screen_get_default(),
GTK_STYLE_PROVIDER(cssProvider),
GTK_STYLE_PROVIDER_PRIORITY_USER);
gtk_box_set_homogeneous (GTK_BOX (main_box), TRUE);
gtk_container_add (GTK_CONTAINER (window), main_box);
gtk_container_add (GTK_CONTAINER (main_box), button_01);
gtk_container_add (GTK_CONTAINER (main_box), label0);
gtk_container_add (GTK_CONTAINER (main_box), label1);
gtk_container_add (GTK_CONTAINER (main_box), label2);
gtk_container_add (GTK_CONTAINER (main_box), button_02);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all (window);
}
int
main (int argc,
char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}
这是css文件:
theme.css
GtkWindow {
font: Comic Sans 12
}
.button {
color: green;
}
代码工作正常,因为所有字体都被更改,所有按钮都是红色。我想要做的是单独设置每个小部件的样式,例如使label0显示为红色,label1显示为绿色,标签2显示为蓝色。
根据这些例子https://developer.gnome.org/gtk3/stable/chap-css-overview.html,这可以解决这个问题:
label#label0 {
color: red;
}
label#label1 {
color: green;
}
label#label2 {
color: blue;
}
但显然这并不意味着我想念那里的东西。
任何关于如何使用css代码单独设置小部件样式的帮助将不胜感激。