Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
866 views
in Technique[技术] by (71.8m points)

how to set a specific css class to a widget in gtk3? (c)

I'm trying css in gtk3 and I don't understand how to use specific class.

C code:

provider = gtk_css_provider_new();
display = gdk_display_get_default();
screen = gdk_display_get_default_screen (display);
gtk_style_context_add_provider_for_screen (screen, GTK_STYLE_PROVIDER(provider), GTK_STYLE_PROVIDER_PRIORITY_USER);
gtk_css_provider_load_from_path(GTK_CSS_PROVIDER(provider),"styles.css",NULL);

enter_button = gtk_button_new_with_label("Print");
g_signal_connect(G_OBJECT(enter_button), "clicked", G_CALLBACK(print_entry_dialog),&t_data);
gtk_box_pack_start(GTK_BOX(hbox3), enter_button, TRUE, TRUE, 0);

Css (styles.css):

GtkButton{
  background: #669999;
  text-shadow: 1px 1px 5px black;
  box-shadow: 0px 0px 5px black;
  border: 1px solid black;
}

In this way it works : enter image description here

But I want to set a 'enter_button' class that set properties for only the 'enter_button', not for all widget under GtkButton name.

I read about gtk_style_context_add_class () func, but I don't know how it works with the 'styles.css' file. What I should do?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I solved in this way:

GtkStyleContext *context;
enter_button = gtk_button_new_with_label("Print");
context = gtk_widget_get_style_context(enter_button);
gtk_style_context_add_class(context,"enter_button");

CSS:

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

For further informations look up here : GtkStyleContext examples


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...