点击后如何在GTK中创建新窗口?

时间:2017-10-29 18:26:19

标签: c gtk3

我是GTK的新手,我想知道如何在点击按钮后创建一个新窗口。我有这个功能

void cb_create_entry(GtkWidget *, gpointer);

int create_window(int argc, char *argv[]){
GtkWidget *p_window = NULL;
GtkWidget *p_main_box = NULL;

GtkWidget *p_button[5];

gtk_init (&argc, &argv);

//Create window
p_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

gtk_window_set_title(GTK_WINDOW(p_window), "Hello");
gtk_window_set_default_size(GTK_WINDOW(p_window), 320, 200);

p_main_box = gtk_vbox_new(TRUE, 0);
gtk_container_add(GTK_CONTAINER(p_window), p_main_box);

{
  p_button[0] = gtk_button_new_with_label("Create entry");
  g_signal_connect(G_OBJECT(p_button[0]), "clicked", 
  G_CALLBACK(cb_create_entry), NULL);
  gtk_box_pack_start(GTK_BOX(p_main_box), p_button[0], FALSE, FALSE, 0);
}
  gtk_widget_show_all(p_window);

  gtk_main ();

  return EXIT_SUCCESS;

和callback.h

#ifndef CALLBACK_H_INCLUDED
#define CALLBACK_H_INCLUDED
#include <gtk/gtk.h>

void cb_create_entry(GtkWidget *p_widget, gpointer user_data){
gtk_button_released(p_widget);
GtkWidget *p_window;
GtkWidget *p_v_box;
GtkWidget *p_entry;

p_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(p_window), "Create DB");
gtk_window_set_default_size(GTK_WINDOW(p_window), 320, 200);

p_v_box = gtk_vbox_new(TRUE, 0);
gtk_container_add(GTK_CONTAINER(p_window), p_v_box);

p_entry = gtk_entry_new();
gtk_box_pack_start(GTK_BOX(p_v_box), p_entry, TRUE, FALSE, 0);
}

和主要

int main(int argc, char const *argv[]) {
create_window(argc, argv);
return 0;
}

但它不起作用。我想用输入创建一个新窗口。但是当我点击按钮时,没有任何反应。

2 个答案:

答案 0 :(得分:2)

我对你如何布置文件结构感到有点困惑。由于没有输入,我将假设b = dict(a.items())函数的文件与export let FormInput = createReactClass({ handleInput(e){ e.preventDefault(); const product = this.name.value; const red = this.red.checked; this.props.addProduct(product,red); this.inputForm.reset(); }, render(){ return( <form className="prod_input" ref={x => this.inputForm = x} onSubmit={this.handleInput}> <input type="text" ref={x => this.name = x} placeholder="Product name"/> <input type="checkbox" ref={x => this.red = x} value="true"/> <input type="submit" hidden/> </form> ) } }) 的文件相同。然后,create_window不应该有实现代码。

尽管如此,我没有看到任何maincallback.h来电,也不确定您是否已经压制或错过了它们。我会假设后者因为您的描述似乎可以看到初始窗口。

它也缺少对gtk_widget_show的调用。

将GtkWidget gtk_widget_show_all函数和gtk_main添加到您的代码中,它确实可以正常工作:

让我们调用第一个文件 show

gtk_main

另一个文件 main.c

#include <gtk/gtk.h>
#include "callback.h"

b_create_entry(GtkWidget *, gpointer);

int create_window(int argc, char *argv[]){
   GtkWidget *p_window = NULL;
   GtkWidget *p_main_box = NULL;

   GtkWidget *p_button[5];

   gtk_init (&argc, &argv);

   //Create window
   p_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

   gtk_window_set_title(GTK_WINDOW(p_window), "Hello");
   gtk_window_set_default_size(GTK_WINDOW(p_window), 320, 200);

   p_main_box = gtk_vbox_new(TRUE, 0);
   gtk_container_add(GTK_CONTAINER(p_window), p_main_box);

   p_button[0] = gtk_button_new_with_label("Create entry");
   g_signal_connect(G_OBJECT(p_button[0]), "clicked", 
   G_CALLBACK(cb_create_entry), NULL);
   gtk_box_pack_start(GTK_BOX(p_main_box), p_button[0], FALSE, FALSE, 0);

   gtk_widget_show_all(p_window);
}

int main (int argc, char *argv[]) {
   create_window(argc, argv);
   gtk_main ();
   return 0;
}

然后编译:

callbacks.h

将生成一个带有按钮的窗口,按下该按钮后将创建并显示一个带有GtkEntry的新窗口:

result

答案 1 :(得分:0)

现在callback.h看起来像这样:

int cb_create_entry(GtkWidget *p_widget, gpointer user_data){
  GtkWidget *p_window = NULL;
  GtkWidget *p_entry = NULL;
  GtkWidget *p_button = NULL;
  GtkWidget *p_main_box = NULL;
  GtkWidget *p_label = NULL;

  p_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

  gtk_window_set_title(GTK_WINDOW(p_window), "Create entry");
  gtk_window_set_default_size(GTK_WINDOW(p_window), 320, 200);
  g_signal_connect (G_OBJECT (p_window), "destroy", G_CALLBACK (cb_quit), NULL);

  p_main_box = gtk_vbox_new(TRUE, 0);
  gtk_container_add(GTK_CONTAINER(p_window), p_main_box);

  p_label = gtk_label_new("Please, name your DB");
  gtk_container_add(GTK_CONTAINER(p_main_box), p_label);

  p_entry = gtk_entry_new();
  gtk_container_add(GTK_CONTAINER(p_main_box), p_entry);

  p_button = gtk_button_new_with_label("Create !");
  gtk_container_add(GTK_CONTAINER(p_main_box), p_button);

  {
      GtkWidget *p_quit = NULL;

      p_quit = gtk_button_new_with_label("Quit");
      g_signal_connect(G_OBJECT(p_quit), "clicked", G_CALLBACK(cb_quit), NULL);
      gtk_box_pack_start(GTK_BOX(p_main_box), p_quit, FALSE, FALSE, 0);
  }
  //gtk_widget_show(p_entry);

  gtk_widget_show_all(p_window);

  return EXIT_SUCCESS;}