10.5. Frames

Frames can be used to enclose one or a group of widgets with a box which can optionally be labeled. The position of the label and the style of the box can be altered to suit.

A Frame can be created with the following function:

  frame = gtk.Frame(label=None)

The label is by default placed in the upper left hand corner of the frame. Specifying a value of None for the label argument or specifying no label argument will result in no label being displayed. The text of the label can be changed using the method.


The position of the label can be changed using this method:

  frame.set_label_align(xalign, yalign)

xalign and yalign take values between 0.0 and 1.0. xalign indicates the position of the label along the top horizontal of the frame. yalign is not currently used. The default value of xalign is 0.0 which places the label at the left hand end of the frame.

The next method alters the style of the box that is used to outline the frame.


The type argument can take one of the following values:

  SHADOW_ETCHED_IN      # the default

The frame.py example illustrates the use of the Frame widget. Figure 10.4, “Frame Example” shows the resulting display:

Figure 10.4. Frame Example

Frame Example

The source code of frame.py is:

    1	#!/usr/bin/env python
    3	# example frame.py
    5	import pygtk
    6	pygtk.require('2.0')
    7	import gtk
    9	class FrameExample:
   10	    def __init__(self):
   11	        # Create a new window
   12	        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
   13	        window.set_title("Frame Example")
   15	        # Here we connect the "destroy" event to a signal handler 
   16	        window.connect("destroy", lambda w: gtk.main_quit())
   17	        window.set_size_request(300, 300)
   19	        # Sets the border width of the window.
   20	        window.set_border_width(10)
   22	        # Create a Frame
   23	        frame = gtk.Frame()
   24	        window.add(frame)
   26	        # Set the frame's label
   27	        frame.set_label("GTK Frame Widget")
   29	        # Align the label at the right of the frame
   30	        frame.set_label_align(1.0, 0.0)
   32	        # Set the style of the frame
   33	        frame.set_shadow_type(gtk.SHADOW_ETCHED_OUT)
   34	        frame.show()
   36	        # Display the window
   37	        window.show()
   39	def main():
   40	    # Enter the event loop
   41	    gtk.main()
   42	    return 0
   44	if __name__ == "__main__":
   45	    FrameExample()
   46	    main()

The calendar.py, label.py and spinbutton.py examples also use Frames.