Unable to get height/width of a widget - gtkmm

2019-04-08 13:28发布

问题:

I'm writing an application using gtkmm.

I wrote a simple widget class, that I want to display in the application's main window only in some cases. Otherwise, I would like a Label "disabled" to be visible.

To achieve that I packed both the widget and the label into one VBox, and I show() and hide() them in order to swap them.

However, the custom widget is far larger than the label, so I need to resize the label a bit.

I know I can use label.set_size_request(x,y), and it works, when I type the dimensions manually. But I am aware, that the widget may differ in size a bit, depending on the Gtk theme one uses, etc.

I'd love to set label exactly the same size *as the widget has. However, using widget.get_height()* does not work - it always returns 1.


Maybe a clue will be that I do this from within the main window's constructor.

回答1:

I haven't done much in gtkmm, more in pygtk. But I think to get meaningful values from get_width/get_height, a widget must be realized, which is an X-window speak for visible. And I guess your widgets are not yet visible in window constructor.

I'd propose you to use a gtk.Notebook instead, with its tabs hidden. Then gtk.Notebook will control size of both your label and your custom widget.