为什么CSS样式未应用于GTK +代码? [英] Why is CSS style not being applied to GTK+ code?

查看:123
本文介绍了为什么CSS样式未应用于GTK +代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始摆弄GTK +,直到尝试修改旋转按钮小部件为止:

I started fiddling with the GTK+ until I tried to modify a spin button widget:

GTK +将字体更改为旋转按钮

我不太清楚答案,但是我开始寻找CSS并尝试代码示例。最后,经过一些谷歌搜索和复制/粘贴代码,尤其是从这里如何在gtk3中为窗口小部件设置特定的CSS类? (c),这是我设法做到的,没有语法或其他错误:

I didn't quite understood the answer, but I started looking for CSS and trying out the code examples. Finally, after some googling and copy / paste code, especially from here how to set a specific css class to a widget in gtk3? (c) , this is what I managed to do without syntactic or other errors:

test.c

#include <gtk/gtk.h>
#include <string.h>  

static void
activate (GtkApplication *app, gpointer user_data) {
  GtkStyleContext *context;
  GtkWidget *button_01;
  GtkWidget *button_02;
  button_01 = gtk_button_new_with_label("This is a simple button");
  button_02 = gtk_button_new_with_label("This is a stylish button");
  context = gtk_widget_get_style_context(button_02);
  gtk_style_context_add_class(context, "my_style");

  GtkWidget *window;
  GtkWidget * main_box;

  window = gtk_application_window_new (app);
  main_box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 25);

  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), button_02);

  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文件:

my_style.css

.my_style{
    background: #669999;
    text-shadow: 1px 1px 5px black;
    border-radius: 3px;
    box-shadow: 0px 0px 5px black;
}

如果有人编译上面的代码,则会出现一个包含两个按钮的窗口,一个按钮根据css文件显示样式。 但是,这两个按钮似乎都是默认样式,就像my_style.css文件被忽略一样。

Should someone compile the code above, a window containing two buttons appears, one button being stylish according to the css file. Yet, both buttons appear to be default styled, as if the my_style.css file is being ignored.

如果有人可以帮助,那

推荐答案

应用程序可以通过调用gtk_css_provider_load_from_file()或gtk_css_provider_load_from_resource()来使GTK +解析特定的CSS样式表。使用gtk_style_context_add_provider()或gtk_style_context_add_provider_for_screen()添加提供程序。以下对象用于实现样式:

An application can make GTK+ parse a specific CSS style sheet by calling gtk_css_provider_load_from_file() or gtk_css_provider_load_from_resource() and adding the provider with gtk_style_context_add_provider() or gtk_style_context_add_provider_for_screen(). The following objects are used to implement the style:


  • GtkStyleContext 是存储样式信息的对象影响由GtkWidgetPath定义的小部件。为了构造最终的样式信息,GtkStyleContext从所有附加的GtkStyleProviders中查询信息。

  • GtkStyleContext is an object that stores styling information affecting a widget defined by GtkWidgetPath. In order to construct the final style information, GtkStyleContext queries information from all attached GtkStyleProviders.

GtkCssProvider 是实现GtkStyleProvider接口的对象。 。 GtkStyleProvider用于向GtkStyleContext提供样式信息

GtkCssProvider is an object implementing the GtkStyleProvider interface. GtkStyleProvider is used to provide style information to a GtkStyleContext

GtkCssProvider 是实现GtkStyleProvider接口的对象。它能够解析类似CSS的输入以设置小部件样式。

GtkCssProvider is an object implementing the GtkStyleProvider interface. It is able to parse CSS-like input in order to style widgets.

有关更多信息,请参见 https://developer.gnome.org/gtk3/stable/GtkCssProvider.html

for more information https://developer.gnome.org/gtk3/stable/GtkCssProvider.html

代码如下:

#include <gtk/gtk.h>

static void
activate (GtkApplication *app, gpointer user_data) {
  GtkStyleContext *context;
  GtkWidget *button_01;
  GtkWidget *button_02;
  button_01 = gtk_button_new_with_label("This is a simple button");
  button_02 = gtk_button_new_with_label("This is a stylish button");

  context = gtk_widget_get_style_context (button_02);

  GtkCssProvider *provider = gtk_css_provider_new ();

  gtk_css_provider_load_from_path (provider,
    "my_style.css", NULL);

  GtkWidget *window;
  GtkWidget *box;

  window = gtk_application_window_new (app);
  box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 25);

  gtk_style_context_add_provider (context,
                                    GTK_STYLE_PROVIDER(provider),
                                    GTK_STYLE_PROVIDER_PRIORITY_USER);

  gtk_box_set_homogeneous (GTK_BOX (box), TRUE);
  gtk_container_add (GTK_CONTAINER (window), box);
  gtk_container_add (GTK_CONTAINER (box), button_01);
  gtk_container_add (GTK_CONTAINER (box), button_02);

  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选择器简称为按钮,代码如下:

the CSS selectors is simply called 'button', the code is the following:

button {
    background: #669999;
    text-shadow: 1px 1px 5px black;
    border-radius: 3px;
    box-shadow: 0px 0px 5px black;
}

button:hover {
  background: #3085a9;
}

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

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