11.5. Frames

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

A Frame can be created with the following method:

Gtk::Frame.new( label )

The label is by default placed in the upper left hand corner of the frame. A value of nil for the label argument will result in no label being displayed. The text of the label can be changed using the next two methods.

Gtk::Frame#set_label( label )
Gtk::Frame#label=( label )

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

Gtk::Frame#set_label_align( xalign, yalign )
Gtk::Frame#label_align=( xalign, yalign )

The xalign and yalign arguments 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.

Gtk::Frame#set_shadow_type( type )
Gtk::Frame#shadow_type=( type )

The type argument can take one of the following values:

Gtk::SHADOW_NONE
Gtk::SHADOW_IN
Gtk::SHADOW_OUT
Gtk::SHADOW_ETCHED_IN (the default)
Gtk::SHADOW_ETCHED_OUT

The following code example illustrates the use of the Frame widget.

require 'gtk2'

Gtk.init
window = Gtk::Window.new( Gtk::Window::TOPLEVEL )
window.set_title( "Frame Example" )
window.signal_connect( "destroy" ) { Gtk.main_quit }
window.set_size_request( 300, 300 )
window.set_border_width( 10 )

# Create our frame
frame = Gtk::Frame.new( nil )
window.add( frame )

# Set the frame's label
frame.set_label( "Ruby-GNOME2 Frame Widget" )

# Align the label at the right of the frame
frame.set_label_align( 1.0, 1.0 )

# Set the style of the frame
frame.set_shadow_type( Gtk::SHADOW_ETCHED_OUT )

window.show_all
Gtk.main

Prev Next