gtkgtk3pygobject

Gtk.Widget.get_allocated_size() returns zero-size Gdk.Rectangle for object created after Gtk.Window.show_all() is called


As the following simple example creates a button in a window and 2 seconds later a timer replaces it with a bigger 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_size_request(100, 50)
        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_size_request(200, 100)
        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()

Unfortunately get_allocated_size returns a zero rect for the larger button after it has been added to the Window even thought show_all is called and the new button is visible.

Also the allocated size returned is the size of the Window not the button, as the console output shows:

0 0 500 200
0 0 0 0

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;
}

Output:

x: 0 y: 0 width: 500 height: 200
x: 0 y: 0 width: 0 height: 0

Update 2

Here is the Python version of the correct program

import gi
import time

gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, GLib

class MyButton(Gtk.Button):
    def __init__(self, label, width, height):
        Gtk.Button.__init__(self)
        self.set_label(label)
        self.connect("size-allocate", self.my_size)
        self.set_size_request(width, height)
        self.set_halign(Gtk.Align.CENTER)
        self.set_valign(Gtk.Align.CENTER)
        
    def my_size(self, obj, rect):
        print("Button dimensions", rect.x, rect.y, rect.width, rect.height)
        
class ButtonWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)
        self.set_default_size(500, 200)
        
        button = MyButton("one", 100, 50)
        self.add(button)
        self.timeout_id = GLib.timeout_add(2000, self.on_timeout, button)

    def on_timeout(self, button):
        self.remove(button)
        button = MyButton("two", 200, 100)
        self.add(button)
        self.show_all()
    
win = ButtonWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()

Solution

  • To solve this issue, connect to the "size-allocate" signal. This will be called when a new region has actually been allocated to the widget. From the handler, you can get that information:

    #include <gtk/gtk.h>
    
    guint timeout_id;
    GtkWidget *window;
    GtkWidget *button;
    
    // Called when dimensions is actually allocated to widget.
    void on_size_allocate(GtkWidget *widget, GtkAllocation *allocation, void *data)
    {
        printf("x: %i y: %i width: %i, height: %i\n", allocation->x, allocation->y, allocation->width, allocation->height);
    }
    
    static gboolean on_timeout(gpointer user_data)
    {
        int baseline;
        GtkAllocation rect;
        GtkWidget *button2;
        
        gtk_container_remove(GTK_CONTAINER(window), button);
        button2 = gtk_button_new_with_label("two");
        g_signal_connect(button2, "size-allocate", G_CALLBACK(on_size_allocate), NULL);
        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(button2);
        
        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");
        g_signal_connect(button, "size-allocate", G_CALLBACK(on_size_allocate), NULL);
        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_FLAGS_NONE);
        g_signal_connect(app, "activate", G_CALLBACK (activate), NULL);
        status = g_application_run(G_APPLICATION (app), argc, argv);
        g_object_unref(app);
    
        return status;
    }
    

    Here is the output I get from this code:

    x: 200 y: 75 width: 100, height: 50
    x: 150 y: 50 width: 200, height: 100
    

    which I think is what you were looking for. From what I understand, the problem with calling gtk_widget_set_size_request and then calling gtk_widget_get_allocated_size is that the size request may not happen immediately. If it has not happened, the values returned by gtk_widget_get_allocated_size are invalid, hence your weird output. By connecting to the signal, you let GTK notify you when the size has actually been allocated, hence the valid values.