编译器可以工作,但不会改变窗口的背景 为什么会这样? 这是我的代码:
#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 :(得分:2)
{@ 1}}函数已被弃用,因此尝试使用不同的方法比调试此函数更好。
相反,创建一个C/O/OU/CN
,将gtk_window_override_background_color
等CSS加载到其中,然后使用GtkCssProvider
将其添加到按钮的样式上下文中。