10.6. Aspect Frames
Prev | Chapter 10. Container Widgets | Next

## 10.6. Aspect Frames

The aspect frame widget is like a frame widget, except that it also enforces the aspect ratio (that is, the ratio of the width to the height) of the child widget to have a certain value, adding extra space if necessary. This is useful, for instance, if you want to preview a larger image. The size of the preview should vary when the user resizes the window, but the aspect ratio needs to always match the original image.

To create a new aspect frame use:

      aspect_frame = gtk.AspectFrame(label=None, xalign=0.5, yalign=0.5, ratio=1.0, obey_child=TRUE)

`label` specifies the text to be displayed as the label. `xalign` and `yalign` specify alignment as with `gtk.Alignment` widgets. If `obey_child` is `TRUE`, the aspect ratio of a child widget will match the aspect ratio of the ideal size it requests. Otherwise, it is given by `ratio`.

To change the options of an existing aspect frame, you can use:

      aspect_frame.set(xalign=0.0, yalign=0.0, ratio=1.0, obey_child=TRUE)

As an example, the aspectframe.py program uses an `AspectFrame` to present a drawing area whose aspect ratio will always be 2:1, no matter how the user resizes the top-level window. Figure 10.5, “Aspect Frame Example” illustrates the display of the program:

Figure 10.5. Aspect Frame Example

The source code for aspectframe.py is:

        1	#!/usr/bin/env python
        2
        3	# example aspectframe.py
        4
        5	import pygtk
        6	pygtk.require('2.0')
        7	import gtk
        8
        9	class AspectFrameExample:
       10	    def __init__(self):
       11	        window = gtk.Window(gtk.WINDOW_TOPLEVEL);
       12	        window.set_title("Aspect Frame")
       13	        window.connect("destroy", lambda x: gtk.main_quit())
       14	        window.set_border_width(10)
       15
       16	        # Create an aspect_frame and add it to our toplevel window
       17	        aspect_frame = gtk.AspectFrame("2x1", # label
       18	                                       0.5, # center x
       19	                                       0.5, # center y
       20	                                       2, # xsize/ysize = 2
       21	                                       False) # ignore child's aspect
       22	        window.add(aspect_frame)
       23	        aspect_frame.show()
       24
       25	        # Now add a child widget to the aspect frame
       26	        drawing_area = gtk.DrawingArea()
       27
       28	        # Ask for a 200x200 window, but the AspectFrame will give us a 200x100
       29	        # window since we are forcing a 2x1 aspect ratio
       30	        drawing_area.set_size_request(200, 200)
       31	        aspect_frame.add(drawing_area)
       32	        drawing_area.show()
       33	        window.show()
       34
       35	def main():
       36	    gtk.main()
       37	    return 0
       38
       39	if __name__ == "__main__":
       40	    AspectFrameExample()
       41	    main()

Prev | Up | Next
10.5. Frames | Home | 10.7. Paned Window Widgets