2016-05-23 259 views
0

編譯器工作但不改變窗口的背景 爲什麼會發生這種情況? 這是我的代碼:在gtk 3.0中改變背景顏色

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

// initialize the window 
void initwindow(float factor,const char* title,GtkWidget* window){ 
    gtk_window_set_default_size(GTK_WINDOW(window), gdk_screen_get_width (gdk_screen_get_default())*factor,gdk_screen_get_height(gdk_screen_get_default())*factor); 
    gtk_window_set_title(GTK_WINDOW(window),title); 
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);} 

// create random doubles 
double randdouble(){ 
    return (double)rand()/(double)RAND_MAX; } 

// function to change backgrounf 

void changwindowcolor(GtkWidget* button){ 
    GdkRGBA color; 
    color.red=randdouble(); 
    color.green=randdouble(); 
    color.blue=randdouble(); 
    color.alpha=1; 
    gtk_widget_override_background_color(button, GTK_STATE_NORMAL,&color);} 

void activate(GtkApplication *app,gpointer user_data){ 
    const char *title="Myapp"; 
    GtkWidget *window= gtk_application_window_new (app); 
    GtkWidget *layout = gtk_grid_new(); 
    GtkWidget *button = gtk_button_new_with_label("color"); 
    initwindow(0.5,title,window); 
    gtk_container_add (GTK_CONTAINER (window), layout); 
    g_signal_connect(button, "clicked", G_CALLBACK(changwindowcolor),button); 
    gtk_grid_attach(GTK_GRID (layout), button, 0,0,1,1); 
    gtk_widget_show_all(window);} 

int main(int argc, char **argv){ 

    GtkApplication *app = gtk_application_new ("org.gtk.example",G_APPLICATION_FLAGS_NONE); 
    g_signal_connect (app, "activate", G_CALLBACK (activate), NULL); 
    int status = g_application_run (G_APPLICATION (app), argc, argv); 
    g_object_unref (app); 
    return status;} 
+0

好吧,嗯......它是做什麼的? –

+0

當你調試它時發生了什麼?當你點擊按鈕時,changwindowcolor()會被調用嗎? –

+0

有惡魔嗎? –

回答

2

gtk_window_override_background_color功能已被棄用,因此它可能是更好地嘗試不同的方法,而不是調試這一個。

取而代之的是,創建一個GtkCssProvider,加載CSS(如* { background-color: #123456; }),並將其添加到gtk_style_context_add_provider的按鈕樣式上下文中。