4

我有以下代码:

GdkColor color;
color.red = 0x0;
color.green = 0x0;
color.blue = 0x0;

gtk_init (&argc, &argv);
window = gtk_dialog_new ();
gtk_widget_modify_bg(window, GTK_STATE_NORMAL, &color);
g_signal_connect (window, "destroy", G_CALLBACK (destroy), NULL);
gtk_window_set_title (GTK_WINDOW (window), "GtkScrolledWindow example");
gtk_container_set_border_width (GTK_CONTAINER (window), 0);
gtk_widget_set_size_request (window, 300, 300);
gtk_window_fullscreen (GTK_WINDOW(window));
scrolled_window = gtk_scrolled_window_new (NULL, NULL);
gtk_widget_modify_bg(scrolled_window, GTK_STATE_NORMAL, &color);
gtk_container_set_border_width (GTK_CONTAINER (scrolled_window), 10);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
gtk_box_pack_start (GTK_BOX (GTK_DIALOG(window)->vbox), scrolled_window, TRUE, TRUE, 0);
gtk_widget_show (scrolled_window);
outside = gtk_table_new (10, 10, FALSE);
gtk_table_set_row_spacings (GTK_TABLE (outside), 10);
gtk_table_set_col_spacings (GTK_TABLE (outside), 10);
gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled_window), outside);
gtk_widget_modify_bg(scrolled_window, GTK_STATE_NORMAL, &color);
gtk_widget_modify_bg(outside, GTK_STATE_NORMAL, &color);
gtk_widget_show (outside);

由于某种原因,窗口而不是滚动窗口的颜色设置为黑色。我怎样才能改变这个?

4

2 回答 2

3

您只需要一个GtkEventBox.

GdkColor color;
color.red = 0x0;
color.green = 0x0;
color.blue = 0x0;

gtk_init (&argc, &argv);
window = gtk_dialog_new ();
gtk_widget_modify_bg(window, GTK_STATE_NORMAL, &color);
g_signal_connect (window, "destroy", G_CALLBACK (destroy), NULL);
gtk_window_set_title (GTK_WINDOW (window), "GtkScrolledWindow example");
gtk_container_set_border_width (GTK_CONTAINER (window), 0);
gtk_widget_set_size_request (window, 300, 300);
gtk_window_fullscreen (GTK_WINDOW(window));
scrolled_window = gtk_scrolled_window_new (NULL, NULL);
//gtk_widget_modify_bg(scrolled_window, GTK_STATE_NORMAL, &color);
gtk_container_set_border_width (GTK_CONTAINER (scrolled_window), 10);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
gtk_box_pack_start (GTK_BOX (GTK_DIALOG(window)->vbox), scrolled_window, TRUE, TRUE, 0);
gtk_widget_show (scrolled_window);
outside = gtk_table_new (10, 10, FALSE);
gtk_table_set_row_spacings (GTK_TABLE (outside), 10);
gtk_table_set_col_spacings (GTK_TABLE (outside), 10);

GtkWidget *box = gtk_event_box_new();
gtk_container_add(GTK_CONTAINER(box), outside);
gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled_window), box);
gtk_widget_modify_bg(box, GTK_STATE_NORMAL, &color);
gtk_widget_show (box);

//gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled_window), outside);
//gtk_widget_modify_bg(scrolled_window, GTK_STATE_NORMAL, &color);
//gtk_widget_modify_bg(outside, GTK_STATE_NORMAL, &color);
gtk_widget_show (outside);
于 2017-05-28T19:39:29.060 回答
-1

如果这是 Gtk3,你应该使用 CSS:https ://developer.gnome.org/gtk3/stable/GtkCssProvider.html

像这样设置滚动窗口的所有孩子的背景颜色:

scrolledwindow * {
  background-color: red;
}

显然,在实际使用中,您可能会通过为您的小部件本身提供一个自定义类来更加具体。

检查检查器以使尝试 CSS 更容易:https ://wiki.gnome.org/Projects/GTK+/Inspector

于 2017-03-06T19:13:24.677 回答