FAQ Index - Search - Recent Changes - Everything - Add entry

<< Previous Entry | FAQ Entry 5.9 | Next Entry >>

5.9. How do I find out the size of a widget?

Use the GtkWidget's allocation attribute, which contains a geometry tuple. Note that the widget must be shown (you may get around calling only realize on that specific widget when using libglade):

  >>> win = gtk.Window()
  >>> win.realize() # or win.show()
  >>> rect = win.allocation
Would return a gdk.Rectangle, which can be accessed by using:

  >>> rect.width, rect.height, rect.x and rect.y
or just convert it to a four sized tuple:

  >>> t = tuple(rect)
Meaning the widget is at the top left of the window, and is 158x22 pixels in size (the default size of a gtk.Entry, by the way)

To find out how high or wide a widget is (or would be) based on a string size and a font, see FAQ 5.16

PyGTK FAQ Wizard | PyGTK Homepage | Feedback to faq at pygtk.org