GTK 使用 glade 4 正确书写的方法

#include 
#include 
#include 
#include 


GtkBuilder *builder;
GObject *window;
GObject *button;
GObject *button2;

static void
print_hello (GtkWidget *widget,
             gpointer   data)
{
  g_print ("Hello World\n");
  printf("fuckyou\n");
  char *buffer = "OK";
  gtk_button_set_label(GTK_BUTTON(button),buffer);
}

static void
print_hello2 (GtkWidget *widget,
             gpointer   data)
{
  g_print ("Hello World\n");
  gtk_button_set_label(GTK_BUTTON(widget),"fuckyou");
}


static void
activate (GtkApplication *app,
          gpointer        user_data)
{
  /* Construct a GtkBuilder instance and load our UI description */
  builder = gtk_builder_new ();
  gtk_builder_add_from_file (builder, "builder.ui", NULL);

  /* Connect signal handlers to the constructed widgets. */
  window = gtk_builder_get_object (builder, "window");
  gtk_window_set_application (GTK_WINDOW (window), app);

  button = gtk_builder_get_object (builder, "button1");
  button2 = gtk_builder_get_object (builder, "button2");


  g_signal_connect (button, "clicked", G_CALLBACK (print_hello2), button2);
  g_signal_connect (button2, "clicked", G_CALLBACK (print_hello), NULL);

  gtk_widget_set_visible (GTK_WIDGET (window), TRUE);

  /* We do not need the builder any more */
  g_object_unref (builder);
}

int
main (int   argc,
      char *argv[])
{
#ifdef GTK_SRCDIR
  g_chdir (GTK_SRCDIR);
#endif

  GtkApplication *app = gtk_application_new ("org.gtk.example", G_APPLICATION_DEFAULT_FLAGS);
  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;
}

builder.ui




  
  
  
    
      
        
          
            button1
            24
            20
          
        
        
          
            button2
            120
            20
          
        
      
    
  

你可能感兴趣的:(linux,学习)