如何使用CSS代码分别设置

如何使用CSS代码分别设置

本文介绍了如何使用CSS代码分别设置gtk小部件的样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GTK +将字体更改为旋转按钮之后,此如何将CSS应用于GTK +代码?问题,我开始了解如何应用css样式转换为GTK代码,但仍有许多令人困惑的细节.

After this GTK+ change font to spin button and this how to apply CSS to GTK+ code? question, I started understanding how to apply a css style to a GTK code, yet there are still a lot confusing details.

这是我用来创建两个按钮和三个标签的代码:

This is the code with which I create two buttons and three labels:

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变为绿色,而label 2变为蓝色.

The code works fine, because all fonts are changed and all buttons are red. What I want to do is to style each widget individually, for example to make the label0 appear red, the label1 appear green and the label 2 appear blue.

根据这些示例 https://developer.gnome.org /gtk3/stable/chap-css-overview.html ,这应该可以解决问题:

According to these examples https://developer.gnome.org/gtk3/stable/chap-css-overview.html , something something this should do the trick:

label#label0 {
  color: red;
}
label#label1 {
  color: green;
}
label#label2 {
  color: blue;
}

但显然不是,这意味着我在这里错过了一些东西.

but apparently it doesn't which means that I miss something there.

任何有关如何使用CSS代码分别设置小部件样式的帮助.

推荐答案

CSS文件不知道您在程序中为变量指定的名称.您必须使用 gtk_widget_set_name() 09-05 00:16