暂时用其他容器更换容器

时间:2011-12-17 10:46:32

标签: c pygtk gtk gtk2

我目前正在编写一个GTK应用程序,它包含一个带有GtkListView和一个按钮的窗口。当用户按下按钮时,将出现某种形式,允许插入要附加到列表中的新数据。

我希望暂时替换GtkListView,而不是为表单打开一个新窗口。我是GTK的新手,所以我想知道是否有惯用的方法来做到这一点。

我目前的猜测是我必须添加一个带有两个隐藏标签的笔记本,一个用于列表,另一个用于表单。无论何时按下按钮,我都会发出focus-tab信号以切换到表单选项卡。

有没有更好的方法来临时替换GtkListView

2 个答案:

答案 0 :(得分:2)

您可以尝试使用gtk_container_add&按钮的回调中gtk_container_remove来实现此目的。将第一个视图添加到容器中。用户点击按钮,删除第一个视图&添加下一个。反之亦然。确保在添加到容器之前添加对窗口小部件的引用,因为如果从容器中移除窗口小部件并且如果没有对窗口小部件的引用,则窗口小部件将被销毁。以下是C中的示例供您参考:

#include <gtk/gtk.h>

static GtkWidget *view0;
static GtkWidget *view1;
static GtkWidget *frame;
static int current_view_id;

static GtkWidget *
create_view0 (void)
{
  GtkWidget *scrolled_window = gtk_scrolled_window_new (NULL, NULL);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
                  GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
  GtkWidget *view0_content = gtk_label_new ("Here is first view");
  gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW
                     (scrolled_window), view0_content);
  gtk_widget_show_all (scrolled_window);
  return scrolled_window;
}

static GtkWidget *
create_view1 (void)
{
  GtkWidget *scrolled_window = gtk_scrolled_window_new (NULL, NULL);
  GtkWidget *view1_content = gtk_label_new ("Here is next view");
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
                  GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
  gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW
                     (scrolled_window), view1_content);
  gtk_widget_show_all (scrolled_window);
  return scrolled_window;
}

static gboolean
change_view (GtkWidget * button, gpointer data)
{
  (void) data;

  if (current_view_id == 0)
    {
      gtk_button_set_label (GTK_BUTTON (button), "Prev");
      gtk_container_remove (GTK_CONTAINER (frame), view0);
      /* Need to add reference as container will own widget ref */
      /* If gtk_container_remove is called with widget's last ref, widget is destroyed */
      g_object_ref (view1);
      gtk_container_add (GTK_CONTAINER (frame), view1);
      current_view_id = 1;
    }
  else
    {
      gtk_button_set_label (GTK_BUTTON (button), "Next");
      gtk_container_remove (GTK_CONTAINER (frame), view1);
      /* Need to add reference as container will own widget ref */
      /* If gtk_container_remove is called with widget's last ref, widget is destroyed */
      g_object_ref (view0);
      gtk_container_add (GTK_CONTAINER (frame), view0);
      current_view_id = 0;
    }

  return FALSE;
}

int
main (void)
{
  GtkWidget *window;
  GtkWidget *vbox;
  GtkWidget *label;
  GtkWidget *button;

  gtk_init (NULL, NULL);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);

  vbox = gtk_vbox_new (FALSE, 5);
  gtk_container_add (GTK_CONTAINER (window), vbox);

  frame = gtk_frame_new (" Sample for view change ");
  view0 = create_view0 ();
  view1 = create_view1 ();

  /* Need to add reference as container will own widget ref */
  /* If gtk_container_remove is called with widget's last ref, widget is destroyed */
  g_object_ref (view0);
  gtk_container_add (GTK_CONTAINER (frame), view0);

  label = gtk_label_new ("View change sample");
  button = gtk_button_new_with_label ("Next");
  g_signal_connect (button, "clicked", G_CALLBACK (change_view), NULL);
  gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
  gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
  gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
  gtk_widget_show_all (window);

  gtk_main ();
  return 0;
} 

希望这有帮助!

答案 1 :(得分:2)

您不需要笔记本来创建临时小部件,您只需使用一个简单的小盒子即可 我使用的是以下内容:

  # displaying the list view
  container = gtk.VBox()
  container.add(tree_view)

  # removing the treeview and adding a form
  container.remove(tree_view)
  container.add(form)
  form.show()