Gtk_container_remove

Hello,

How can I find which GtkWidget to remove here in my code?
I want the new label in my button that i create with pango markup
language. The code compiles and runs but get errors:

(ColdStore:3173): Gtk-WARNING **: Attempting to add a widget with type GtkLabel to a GtkButton, but as a GtkBin subclass a GtkButton can only contain one widget at a time; it already contains a widget of type GtkLabel
hello

Here is my relevant snippet:

GtkWidget *text;
label = g_strdup (gtk_button_get_label (GTK_BUTTON (buttons[i])));
text = gtk_label_new(NULL);
gtk_label_set_markup (GTK_LABEL (text), "<span foreground=\"green\">1402</span>") ;
/* gtk_button_set_label (GTK_BUTTON (buttons[i]), text); */
gtk_container_add (GTK_CONTAINER(buttons[i]), text);
if ( i < 11 )
	gtk_button_set_label (GTK_BUTTON (buttons[i+1]), old);
old = label ;
--i;
/* g_free() */
if (i < 0)
	return FALSE;
return TRUE;

So i think before gtk_container_add I have to remove the GtkLabel from the container.

PS: How to move the post to Platform,newcomer,gtk not Core? :thinking:

You’re trying to add the same label—old = label—to different widgets—gtk_button_set_label (buttons[i+1], old)—which is something you cannot do. A widget must have a single parent.

From the snippet you pasted I honestly don’t understand what you’re trying to achieve.

:thinking:

o.k. here is the whole code

#include <stdlib.h>
#include <gtk/gtk.h>
#include <stdio.h>
#include <time.h>


#define FREQUENCY 1

static gboolean update_text(GtkWidget **buttons);

int
main (int argc, char *argv[])
{
  int pct = 50;
  char filename[] = "foo.txt";
  printf ("Processing of `%s' is %d%% finished.\nPlease be patient.\n",	filename, pct);
  clock_t start, end;
  double cpu_time_used;
  start = clock();
  gtk_init (&argc, &argv);
  GtkWidget  *window, *vbox, *buttons[12], *frame;
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title(GTK_WINDOW(window), "FA1");
  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  gtk_window_set_default_size(GTK_WINDOW(window), 200, 400);
  g_signal_connect (window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 2);
  frame = gtk_frame_new (NULL);
  buttons[0] = gtk_button_new_with_label ("1404");
  gtk_box_pack_start (GTK_BOX(vbox), buttons[0], FALSE, FALSE, 0);
  buttons[1] = gtk_button_new_with_label ("1403");
  gtk_box_pack_start (GTK_BOX(vbox), buttons[1], TRUE, FALSE, 0);
  buttons[2] = gtk_button_new_with_label ("1402");
  gtk_box_pack_start (GTK_BOX(vbox), buttons[2], TRUE, FALSE, 0);
  /* buttons[2] = gtk_button_new ();
  labels[2] = gtk_label_new (NULL);
  gtk_label_set_markup (GTK_LABEL (labels[2]), "<span foreground=\"red\">1402</span>") ;
  gtk_container_add (GTK_CONTAINER(buttons[2]), labels[2]);
  gtk_box_pack_start (GTK_BOX(vbox), buttons[2], TRUE, TRUE, 0);
  buttons[3] = gtk_button_new ();
  labels[3] = gtk_label_new (NULL);
  gtk_label_set_markup (GTK_LABEL (labels[3]), "<span foreground=\"green\">1401</span>") ;
  gtk_container_add (GTK_CONTAINER(buttons[3]), labels[3]);
  gtk_box_pack_start (GTK_BOX(vbox), buttons[3], FALSE, TRUE, 0); */
  buttons[3] = gtk_button_new_with_label ("1401");
  gtk_box_pack_start (GTK_BOX(vbox), buttons[3], TRUE, FALSE, 0);
  buttons[4] = gtk_button_new_with_label ("1304");
  gtk_box_pack_start (GTK_BOX(vbox), buttons[4], FALSE, TRUE, 0);
  buttons[5] = gtk_button_new_with_label ("1303");
  gtk_box_pack_start (GTK_BOX(vbox), buttons[5], FALSE, TRUE, 0);
  buttons[6] = gtk_button_new_with_label ("1302");
  gtk_box_pack_start (GTK_BOX(vbox), buttons[6], FALSE, TRUE, 0);
  buttons[7] = gtk_button_new_with_label ("1301");
  gtk_box_pack_start (GTK_BOX(vbox), buttons[7], FALSE, TRUE, 0);
  buttons[8] = gtk_button_new_with_label ("1202");
  gtk_box_pack_start (GTK_BOX(vbox), buttons[8], FALSE, TRUE, 0);
  buttons[9] = gtk_button_new_with_label ("1201");
  gtk_box_pack_start (GTK_BOX(vbox), buttons[9], FALSE, TRUE, 0);
  buttons[10] = gtk_button_new_with_label ("1102");
  gtk_box_pack_start (GTK_BOX(vbox), buttons[10], FALSE, TRUE, 0);
  buttons[11] = gtk_button_new_with_label ("1101");
  buttons[12] = gtk_button_new ();
  gtk_box_pack_start (GTK_BOX(vbox), buttons[11], FALSE, TRUE, 0);
  gtk_container_add (GTK_CONTAINER (frame), vbox);
  gtk_container_add (GTK_CONTAINER(window), frame);
  g_timeout_add_seconds(FREQUENCY, (GSourceFunc)update_text, buttons);
  gtk_widget_show_all (window);
  gtk_main ();


  puts("hello");
  end = clock();
  cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
  printf("\nCPU-Time :%f\n", cpu_time_used);
  puts(filename);
  return EXIT_SUCCESS;
}

static gboolean
update_text (GtkWidget **buttons)
{
  static int i = 11;
  static gchar *label, *old ;
  GtkWidget *text;
  label = g_strdup (gtk_button_get_label (GTK_BUTTON (buttons[i])));
  text = gtk_label_new(NULL);
  gtk_label_set_markup (GTK_LABEL (text), "<span foreground=\"green\">Cheesburger</span>") ;
  /* gtk_button_set_label (GTK_BUTTON (buttons[i]), "Cheesburger"); */
  gtk_container_add (GTK_CONTAINER(buttons[i]), text);
  if ( i < 11 )
	  gtk_button_set_label (GTK_BUTTON (buttons[i+1]), old);
  old = label ;
  --i;
  /* g_free() */
  if (i < 0)
	return FALSE;
  return TRUE;
}

So hopefuly my goal is clear. I want the Buttons Label in the vbox to change (the color, size, font) with pango markup.
:face_with_monocle:

gtk_bin_get_child()

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.