O'Reilly logo

Mastering Perl/Tk by Nancy Walsh, Stephen Lidie

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Size of Widget

You can use the following methods to find out the size of a widget in several different ways.

Widget’s Geometry

The geometry method returns the geometry string for the widget in the form of width x height + x + y.

$geom = $widget->geometry;

The geometry string was discussed in detail in Chapter 11. Geometry values are typically specified in pixels. An exception is a child widget that uses characters as its normal units, such as a Text or Listbox, with -setgrid => 1 specified. In this case, the window manager forces a geometry in integral lines and characters.

Requested Height

The reqheight method returns the height of the widget:

$height = $widget->reqheight;

Requested Width

The reqwidth method returns the width of the widget:

$width = $widget->reqwidth;

Actual Width

To get the width of the widget as it currently is drawn, use the widthmethod:

$cur_width = $widget->width;

When the widget is first created, width will return a 1 until the application has finished drawing everything. After that, it will return the actual width of the widget.

Actual Height

To get the current height, use the height method:

$h = $widget->height;

Just like the width method, height returns a 1 when the widget is first created. You can use the update or afterIdle method to force everything else to happen and then call height or width to get the finished values.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required