-->

Previous | Table of Contents | Next

Page 522

foreach i {raised sunken flat groove ridge} {

label .$i -relief $i -text $i;

.$i configure -height 5 -width 5 -borderwidth 5;

pack .$i -side left -padx 5 -pady 5 -fill both -expand 1;

}

The result will be similar to Figure 25.4.


Figure 25.4.
Labels of varying
relief, version 3.

This example can be easily changed to use any of the widget types by replacing label with a different type of widget.

A tcl/tk Interface to xsetroot

This section introduces some of the other abilities of the tk toolkit by applying them to the development of a GUI front end for the X Window program xsetroot.

Most X Window users will be familiar with the X Window program xsetroot that can be used to set the background color of the root window, under X11. The actual command is

xsetroot -solid color

where color can be given in the form #RRGGBB. The front end will allow for colors to be previewed and then applied.

Let's get started. The first thing that you need is a variable that holds the color. Then you need to create two basic frames, one for the main area of the application and another for putting messages in. You will also need a third frame for the controls.

Frames are a handy thing to use because they can be used to pack items of a particular type/function together. Also, they are useful in partitioning up a window into sections that don't change.

Page 523

Create the frames and the color globally:

set color "#000000";

frame .main_frame;

frame .message_frame;

frame .control_frame;

Now pack the frames:

pack .control_frame -in .main_frame -expand 1 -fill both;

pack .main_frame -anchor c -expand 1;

pack .message_frame -anchor s -padx 2 -pady 2 \

-fill x -expand 1;

You also need to create a label to handle messages. I'll do this as a procedure, so it will be easy to modify and execute:

proc make_message_label {} {

label .message_label -relief sunken;

pack .message_label -anchor c \

-in .message_frame -padx 2 \

-pady 2 -fill both -expand 1;

}

Pack the message label into .message_frame so that it is at the bottom of the window at all times.

Now make the scales. You need three scales, one for red, blue, and green, with each one going from 0 to 255. You also need to pack the scales and their corresponding labels in their own frame:

proc make_scales {} {

frame .scale_frame;

foreach i {red green blue} {

frame .scale_frame_$i -bg $i;

label .label_$i -text $i -bg $i \

-fg white;

scale .scale_$i -from 0 -to 255 \

-command setColor;

pack .label_$i .scale_$i \

-in .scale_frame_$i;

pack .scale_frame_$i -in .scale_frame \

-side left -padx 2 -pady 2;

}

pack .scale_frame -in .control_frame \

-side left -expand 1;

}

This procedure is a good example of using frames. In all, this example creates four frames, one for each slider and label pair and overall frame. Adding the label and the slider to their own frame simplifies the overall layout strategy.

Another example in this procedure is the use of the -command option for a widget. Each time the scales change, the command specified by the -command option is executed. In this case, the setColor command, which sets the global variable color, is executed:

Page 524

proc setColor {value} {

global color;

foreach i {red green blue} {

set $i [format %02x [.scale_$i get]];

}

set color "#$red$green$blue";

.preview_label configure -bg $color;

.message_label configure -text "$color";

}

You can preview the color change by setting the background color of the widget, .preview_label. To create .preview label, use the following procedure:

proc make_preview {} {

global color;



frame .preview_frame;

label .preview_label -bg $color \

-height 5 -width 5;

pack .preview_label -in .preview_frame \

-padx 2 -pady 2 -fill both \

-anchor c -expand 1;

pack .preview_frame -in .control_frame \

-side bottom -fill both -expand 1 \

-padx 2 -pady 2;

}

Now you need to add a few buttons—one to apply the changes, another to quit the program. Use the following procedure:

proc make_buttons {} {

frame .button_frame;

button .apply -text "apply" \

-command setRootColor;

button .quit -text "quit" -command exit;

pack .apply .quit -in .button_frame \

-fill both -expand 1 -padx 2 \

-pady 2 -side left;

pack .button_frame -in .main_frame \

-fill both;

}

You also need the following procedure, which sets the root color:

proc setRootColor {} {

global color;



catch {

exec xsetroot -solid $color;

} msg;



if {$msg != {}} {

set msg "An error occurred";

} else {

set msg "$color";

}



.message_label configure -text $msg;

}

Page 525

Now that you are done with the procedures, invoke them:

make_message_label;

make_scales;

make_preview;

make_buttons;

You are now ready to test your little tcl application. When the application is run, the resulting window should look like Figure 25.5.


Figure 25.5.
The tksetroot
application.

Listing 25.1 contains the complete source code of the tksetroot application.

Listing 25.1. tksetroot.

set color "#000000";

frame .main_frame;

frame .message_frame;

frame .control_frame;



pack .control_frame -in .main_frame -expand 1 -fill both;

pack .main_frame -anchor c -expand 1;

pack .message_frame -anchor s -padx 2 -pady 2 \

-fill x -expand 1;



proc make_message_label {} {

label .message_label -relief sunken;

pack .message_label -anchor c -in .message_frame \

-padx 2 -pady 2 -fill both -expand 1;

}



proc make_scales {} {

frame .scale_frame;

foreach i {red green blue} {

frame .scale_frame_$i -bg $i;

label .label_$i -text $i -bg $i -fg white;

scale .scale_$i -from 0 -to 255 \

-command setColor;

pack .label_$i .scale_$i -in .scale_frame_$i;

pack .scale_frame_$i -in .scale_frame -side left \

-padx 2 -pady 2;

}

pack .scale_frame -in .control_frame -side left -expand 1;

}




                                       continues

Previous | Table of Contents | Next