Home > Back-end >  how to change multiple widgets property on button click GTK c
how to change multiple widgets property on button click GTK c

Time:09-17

I am trying to make UI using GTK in c for raspberry pi 4. I want to change the visibility of different widgets based on button click just to simulate a new page. I have tried everything available on the internet but as I am not that good at coding I cant figure out what is wrong. can someone please help ?

This program compiles but when I press the button it gives error " assertion failed on gtk_widget_show " and also on widget hide. Also a segmentation fault occurs and the program crashes.

I am using cmake to compile my code. I have attached the error screen shot.

#include <gtk/gtk.h>

typedef struct AppData 
{
    GtkWidget *label1;
    GtkWidget *label2;
} AppData;

static void button1 (gpointer data)
{  
  AppData *data2 = (AppData*)data;
  gtk_widget_hide(data2->label1);
  gtk_widget_show(data2->label2);
}

static void button2 ( gpointer data)
{
  AppData *data2 = (AppData*)data;
  gtk_widget_show(data2->label1);
  gtk_widget_hide(data2->label2);
}

int main(int argc, char *argv[])
{
  GtkWidget *window;
  GtkWidget *fixed;
  
  GtkWidget *btn1;
  GtkWidget *btn2;
  
  GtkWidget *box1;
  GtkWidget *box2;
  
  gtk_init(&argc, &argv);
  
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title(GTK_WINDOW(window), "ethercat test 1");
  gtk_window_set_default_size(GTK_WINDOW(window), 1000,500);
  
  fixed = gtk_fixed_new();
  gtk_container_add(GTK_CONTAINER(window), fixed);
  
  box1 = gtk_box_new(GTK_ORIENTATION_VERTICAL, 1);
  gtk_fixed_put(GTK_FIXED(fixed), box1, 0,0);
  
  box2 = gtk_box_new(GTK_ORIENTATION_VERTICAL, 1);
  gtk_fixed_put(GTK_FIXED(fixed), box2, 100,100);
  
  AppData *app_data = g_new0 (AppData, 2);

  app_data->label1 = gtk_label_new("label1");
  gtk_box_pack_start(GTK_BOX(box1),app_data->label1, TRUE,TRUE,0);
  
  app_data->label2 = gtk_label_new("label2");
  gtk_box_pack_start(GTK_BOX(box2),app_data->label2, TRUE,TRUE,0);
  
  btn1 = gtk_button_new_with_label("ethercat 1");
  gtk_fixed_put(GTK_FIXED(fixed), btn1, 10, 450);
  gtk_widget_set_size_request(btn1, 80,30);
 
  btn2 = gtk_button_new_with_label("ethercat 2");
  gtk_fixed_put(GTK_FIXED(fixed), btn2, 110, 450);
  gtk_widget_set_size_request(btn2, 80,30);

  gtk_widget_show_all(window);
  
  g_signal_connect(G_OBJECT(btn1), "clicked", G_CALLBACK(button1), app_data);
  g_signal_connect(G_OBJECT(btn2), "clicked", G_CALLBACK(button2), app_data);
  
  g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
  
  gtk_main();
  
  printf("program end\n");
  return (0);
  
}

enter image description here

CodePudding user response:

The function signature of your "clicked" callbacks is wrong. It should be of the form as described in the documentation:

void on_clicked(
  GtkButton* self,
  gpointer user_data
)

So for example, your button2() function becomes

static void button2 (GtkButton *btn2, gpointer data)
{
  AppData *data2 = (AppData*)data;
  gtk_widget_show(data2->label1);
  gtk_widget_hide(data2->label2);
}
  • Related