我正在尝试使用CSS更改按钮颜色,问题是只有当我选择其他应用程序时才有效。当我选择我的应用程序时,所有CSS更改都会消失。我找不到解决这个问题的办法,任何帮助都将不胜感激。我已经附上了我的简化代码和结果。感谢
`
#include <gtk/gtk.h>
#include <string.h>
int main (int argc, char *argv[])
{
gtk_init(&argc,&argv);
GtkWidget* window;
GtkWidget *button_quit;
GtkWidget *grid;
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
grid = gtk_grid_new ();
gtk_container_add (GTK_CONTAINER (window), grid);
// styling background color to black
static const char *css =
"#button_quit{"
"background-color: darkgoldenrod;"
"color: black;"
"font-family: darkseagreen;"
"font-style: normal;"
"font-weight: bold;"
"font-size: 30px;"
"border-radius: 5px;"
"}";
GtkCssProvider* provider = gtk_css_provider_new();
GdkDisplay* display = gdk_display_get_default();
GdkScreen* screen = gdk_display_get_default_screen(display);
gtk_css_provider_load_from_data(GTK_CSS_PROVIDER(provider),css,-1, NULL);
gtk_style_context_add_provider_for_screen(screen,
GTK_STYLE_PROVIDER(provider),
GTK_STYLE_PROVIDER_PRIORITY_USER);
g_object_unref(provider);
//create quit button
button_quit = gtk_button_new_with_label("Quit");
gtk_widget_set_name(button_quit, "button_quit");
g_object_set (button_quit, "margin", 10, NULL);
g_signal_connect(G_OBJECT(button_quit), "clicked",
G_CALLBACK(gtk_main_quit), G_OBJECT(window));
gtk_grid_attach (GTK_GRID (grid), button_quit, 0, 0, 1, 1);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
`
图片:
我的应用程序未选择
我的应用程序已选择
我的css样式是作为背景状态加载的?。我在新定义之前插入了一个新的通用按钮样式,它解决了问题。不知道为什么这样解决了它,但它确实解决了。
button {
background-image: none;
background-color: @bg_color;
}