我想在GTK +程序中使用gtkButton添加一些颜色,但是它显示了一些声明错误,正如我在上面指定的那样。这是声明,我在gtk程序中使用过。
static GdkColor colorRed;//error line 1
gdk_color_parse("red", &colorRed); //error line 2
button1 = gtk_button_new_with_label("button");
gtk_widget_modify_base (button1, GTK_STATE_NORMAL, &colorRed);
但它显示错误
error: expected declaration specifiers or ‘...’ before string constant //line 1
error: expected declaration specifiers or ‘...’ before ‘&’ token //line 2
答案 0 :(得分:1)
gtk_widget_modify_base
自3.0起已被弃用。请改用gtk_widget_override_background_color
。您不需要GdkColor
,只需GdkRGBA
,这对cairo来说更方便。
关于你的错误:我认为你专注于错误的部分。你的片段在这里,编译器会看到错误,但以前的行总是受欢迎的,我认为你的问题是你没有包含GdkColor
的标题。
答案 1 :(得分:0)
为按钮着色的声明是正确的,但我已将其声明为全局,即在main()函数之外,以在多个函数中使用颜色。因此,当我将声明放在main()函数的一边并将gtk_widget_modify_base()替换为gtk_widget_modify_bg()时,它的工作正常。
这是更正后的代码
#include <gtk/gtk.h>
static void destroy (GtkWidget *window, gpointer data);
GtkWidget *window;
GtkWidget *table;
GtkWidget *button;
GtkWidget *button1[20][20];
GtkWidget *button2;
GtkWidget *button3;
GtkWidget *title;
GtkWidget *label;
static char *values[100] =
{ "127.0.0.1", "Idle",
"192.168.73.129", "Idle",
"192.168.73.130", "Idle",
"192.168.73.131", "Idle",
"192.168.73.132", "Idle",
"192.168.73.133", "Idle",
"192.168.73.134", "Idle",
};
int main(int argc, char *argv[])
{ gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL); //gtk_scrolled_window_new(NULL, NULL);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(window), 550, 700);
gtk_window_set_title(GTK_WINDOW(window), "Client Activity Monitor");
gtk_container_set_border_width(GTK_CONTAINER(window), 25);
g_signal_connect (G_OBJECT (window), "destroy",G_CALLBACK (destroy), NULL);
table = gtk_table_new(4, 2, TRUE);
gtk_table_set_row_spacings(GTK_TABLE(table), 2);
gtk_table_set_col_spacings(GTK_TABLE(table), 2);
GdkColor colorRed;//color declaration
gdk_color_parse("red", &colorRed);//color assignment
int i = 0;
int j = 0;
int pos = 0;
title = gtk_frame_new("Client Logs");
label = gtk_label_new("server: waiting for connections...\n");
gtk_frame_set_shadow_type(GTK_FRAME(title), GTK_SHADOW_IN);
gtk_table_attach_defaults(GTK_TABLE(table), title, 0, 1, 0, 1);
button3 = gtk_button_new_with_label("Start Server");
gtk_widget_modify_fg(button3, GTK_STATE_NORMAL, &colorRed);//color use
gtk_table_attach_defaults(GTK_TABLE(table), button3, 1, 2, 0, 1);
gtk_container_add (GTK_CONTAINER (title), label);
gtk_widget_show_all(title);
for(i=0; i < 6; i++) {
for( j=0; j < 2; j++) {
button1[i][j] = gtk_button_new_with_label(values[pos]);
gtk_widget_modify_fg(button1[i][j], GTK_STATE_NORMAL, &colorRed);
gtk_table_attach_defaults(GTK_TABLE(table), button1[i][j], j, j+1, i+1, i+1+1 );
pos++;
}
}
gtk_container_add(GTK_CONTAINER(window), table);
g_signal_connect_swapped (G_OBJECT (button3), "clicked",G_CALLBACK (destroy),(gpointer) window);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
static void destroy (GtkWidget *window, gpointer data)
{
gtk_main_quit ();
}