2017-12-18 368 views
0

有誰知道如何使用css更改按鈕顏色?我需要根據函數是否返回true/false來更改它。 我發現這段代碼,但我不能改變按鈕的顏色。按鈕的顏色變成藍色。gtk +更改按鈕使用css

GdkDisplay *display; 
GdkScreen *screen; 
GtkCssProvider *provider; 
GtkWidget *testBtn; 

//create a button 
testBtn = gtk_button_new_with_label("test"); 
//give button ID 
gtk_widget_set_name(GTK_WIDGET(testBtn), "testBtnNeutral"); 

//Create objects. 
provider = gtk_css_provider_new(); 
display = gdk_display_get_default(); 
screen = gdk_display_get_default_screen(display); 

//Connect screen with provider. 
gtk_style_context_add_provider_for_screen(screen, 
GTK_STYLE_PROVIDER(provider), GTK_STYLE_PROVIDER_PRIORITY_USER); 
//Give button color. 
gtk_css_provider_load_from_data(GTK_CSS_PROVIDER(provider), "#testBtnNeutral {background:blue; color:white} \n #testBtnFail {background:red; color:white} \n #testBtnSuccess {background:green; color:white}", -1 , NULL); 

函數,我試圖改變按鈕的顏色。

static void testFunction(VariablesThatCouldBeUsedInFunctions *StructPointer){ 
     if(bTestElectricFunctions){ 
     gtk_widget_set_name(GTK_WIDGET(StructPointer->testBtn), "testBtnSuccess"); 
    //Gtk-CRITICAL **: gtk_widget_set_name: assertion 'GTK_IS_WIDGET (widget)' failed 
    } 
else{ 
    printf("untrue\n"); 
    } 
} 
+0

是指這樣的:https://stackoverflow.com/questions/1706550/gtk-modifying-background-color-of-gtkbutton – 2017-12-18 13:58:47

+0

對不起,我讀過,但我不明白這一點。我如何製作這樣的代碼。 If(function == true){buttoncolor = red; } 使用gtk + 3。 – Linusovic

+0

你調用'gtk_widget_set_name'將哪個部件的名稱更改爲與其他風格相匹配的部件在哪裏? – liberforce

回答

0

下面的代碼片段按預期工作,你可能在你的testFunction或調用者的問題,關鍵的錯誤是很清楚的,StructPointer->testBtn是什麼,但一個GtkWidget的,也許是NULL

#include <gtk/gtk.h> 

int buttonState = 2; 

static void button_cb (GtkWidget *widget, gpointer data) 
{ 
    if (buttonState == 0) 
    gtk_widget_set_name (widget, "btnFail"); 
    else if (buttonState == 1) 
    gtk_widget_set_name (widget, "btnNeutral"); 
    else 
    gtk_widget_set_name (widget, "btnSuccess"); 

    buttonState = buttonState == 2 ? 0 : ++buttonState; 
} 

int main (int argc, char **argv) 
{ 
    gtk_init (&argc, &argv); 

    GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL); 
    gtk_window_set_default_size (GTK_WINDOW (window), 200, 200); 
    g_signal_connect(window, "destroy", G_CALLBACK (gtk_main_quit), NULL); 

    GtkWidget *button = gtk_button_new_with_label ("Click Me"); 
    gtk_widget_set_name (button, "btnNeutral"); 
    gtk_widget_set_valign (button, GTK_ALIGN_CENTER); 
    gtk_widget_set_halign (button, GTK_ALIGN_CENTER); 
    g_signal_connect (button, "clicked", G_CALLBACK (button_cb), NULL); 
    gtk_container_add (GTK_CONTAINER (window), button); 

    GtkCssProvider *provider = gtk_css_provider_new(); 
    GdkDisplay *display = gdk_display_get_default(); 
    GdkScreen *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_data (GTK_CSS_PROVIDER (provider), "#btnNeutral {background:blue; color:white} #btnFail {background:red; color:white} #btnSuccess {background:green; color:white}", -1 , NULL); 

    gtk_widget_show_all (window); 

    gtk_main(); 

    return 0; 
} 
+0

是的,你是對的。我做了錯誤的功能。一旦我糾正它,它就像一個魅力。 – Linusovic