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.

  frame.set_label(label)

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.

  frame.set_shadow_type(type)

The type argument can take one of the following values:

  SHADOW_NONE
  SHADOW_IN
  SHADOW_OUT
  SHADOW_ETCHED_IN      # the default
  SHADOW_ETCHED_OUT

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
    2	
    3	# example frame.py
    4	
    5	import pygtk
    6	pygtk.require('2.0')
    7	import gtk
    8	
    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")
   14	
   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)
   18	
   19	        # Sets the border width of the window.
   20	        window.set_border_width(10)
   21	
   22	        # Create a Frame
   23	        frame = gtk.Frame()
   24	        window.add(frame)
   25	
   26	        # Set the frame's label
   27	        frame.set_label("GTK Frame Widget")
   28	
   29	        # Align the label at the right of the frame
   30	        frame.set_label_align(1.0, 0.0)
   31	
   32	        # Set the style of the frame
   33	        frame.set_shadow_type(gtk.SHADOW_ETCHED_OUT)
   34	        frame.show()
   35	  
   36	        # Display the window
   37	        window.show()
   38	
   39	def main():
   40	    # Enter the event loop
   41	    gtk.main()
   42	    return 0
   43	
   44	if __name__ == "__main__":
   45	    FrameExample()
   46	    main()

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