The following example creates a center-aligned button in a Window. Two seconds later this button is deleted and replaced by another button.
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, GLib
class ButtonWindow(Gtk.Window):
def __init__(self):
super().__init__()
self.set_default_size(500, 200)
button = Gtk.Button.new_with_label("one")
button.set_halign(Gtk.Align.CENTER)
button.set_valign(Gtk.Align.CENTER)
self.add(button)
self.timeout_id = GLib.timeout_add(2000, self.on_timeout, button)
def on_timeout(self, button):
rect, _ = button.get_allocated_size()
print(rect.x, rect.y, rect.width, rect.height)
self.remove(button)
button = Gtk.Button.new_with_label("two")
button.set_halign(Gtk.Align.CENTER)
button.set_valign(Gtk.Align.CENTER)
self.add(button)
self.show_all()
rect, _ = button.get_allocated_size()
print(rect.x, rect.y, rect.width, rect.height)
win = ButtonWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
Gtk.Widget.get_allocated_size()
returns an incorrect size rect 0 0 500 200
(size of the Window) for the old button but 0 0 0 0
for the new button even after Gtk.Window.show_all()
is called.
What am I doing wrong here?
Update
The behaviour is identical in the native GTK3 API
#include <gtk/gtk.h>
guint timeout_id;
GtkWidget *window;
GtkWidget *button;
static gboolean on_timeout(gpointer user_data)
{
int baseline;
GtkAllocation rect;
GtkWidget *button2;
gtk_widget_get_allocated_size(button, &rect, &baseline);
printf("x: %i y: %i width: %i height: %i\n", rect.x, rect.y, rect.width, rect.height);
gtk_container_remove(GTK_CONTAINER(window), button);
//g_object_unref (button); // segfault???
button2 = gtk_button_new_with_label ("two");
gtk_widget_set_size_request (button2, 200, 100);
gtk_widget_set_halign (button2, GTK_ALIGN_CENTER);
gtk_widget_set_valign (button2, GTK_ALIGN_CENTER);
gtk_container_add (GTK_CONTAINER(window), button2);
gtk_widget_show_all (window);
gtk_widget_get_allocated_size(button2, &rect, &baseline);
printf("x: %i y: %i width: %i height: %i\n", rect.x, rect.y, rect.width, rect.height);
g_source_remove(timeout_id);
return 0;
}
static void activate(GtkApplication* app, gpointer user_data)
{
window = gtk_application_window_new (app);
gtk_window_set_default_size (GTK_WINDOW (window), 500, 200);
button = gtk_button_new_with_label ("one");
gtk_widget_set_size_request (button, 100, 50);
gtk_widget_set_halign (button, GTK_ALIGN_CENTER);
gtk_widget_set_valign (button, GTK_ALIGN_CENTER);
gtk_container_add (GTK_CONTAINER(window), button);
gtk_widget_show_all (window);
timeout_id = g_timeout_add(2000, on_timeout, NULL);
}
int main(int argc, char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new ("org.gtk.example", G_APPLICATION_DEFAULT_FLAGS);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}
The output
x: 0 y: 0 width: 500 height: 200
x: 0 y: 0 width: 0 height: 0