-->
Previous Table of Contents Next


The Tcl script that makes all this happen appears here:


     #!/usr/bin/wish

     #

     # toolbar.tcl

     # Tcl script that puts a toolbar to launch

     # programs at the bottom of the screen.



     #

     # Executes a command as a UNIX process.

     #

     proc exec_cmd { command } {



         # Execute as a UNIX process in the background.

         # We use eval to handle the messy details of

         # separating the command into its elements.

         # (Try it without eval and you'll see why.)

         #

         eval exec $command &

     }

     #



     # Tcl/Tk procedure to place the

     # current time in a widget. With Tcl 7.5,

     # you can also use the clock command.

     #

     set title_interval  40000

     set time_command    "/bin/date \"+%I:%M %p\" "



     global title_interval time_command

     proc update_time { butn } {

         global  title_interval time_command



         # Get current time.

         set timestr [ eval exec $time_command ]



         $butn config -text $timestr



         # Set up command to run again.

         after $title_interval " update_time $butn"

     }



     #

     # Global commands to execute when

     # toolbar buttons get pushed.

     #

     set cmds(man)   "/usr/bin/X11/xman -notopbox -bothshown"

     set cmds(mail)  "/usr/bin/X11/xterm -ls -e elm"

     set cmds(term)  "/usr/bin/X11/xterm -ls"

     set cmds(file)  "/usr/bin/X11/xfm"

     set cmds(xv)    "/usr/bin/X11/xv"

     set cmds(xmah)  "/usr/bin/X11/xmahjongg"



     # Make cmds array global.

     global cmds

     #



     # Main program.

     #

     #   Set window manager values.

     wm geometry         .   +0-0

     wm overrideredirect . true



     #

     # Frame to hold everything.

     #

     set back lightgray

     frame .frame -relief raised -bd 2 -bg $back

     .frame config -cursor top_left_arrow



     #

     # Logo/Name widget.

     #

     set title [format "Linux: %s" [ exec hostname ] ]



     button .frame.logo -text $title \

       -command { exit } -bg $back \

       -relief flat -padx 8



     pack .frame.logo -side left -fill y



     #

     # Create other widgets

     # that make up our toolbar.

     #

     button .frame.man -text "Manuals" \

      -command { exec_cmd $cmds(man) } \

      -relief flat -padx 8 -bg $back



     button .frame.mail -text "Mail" \

      -command { exec_cmd $cmds(mail) } \

      -relief flat -padx 8 -bg $back



     button .frame.term -text "Shell" \

      -command { exec_cmd $cmds(term) } \

      -relief flat -padx 8 -bg $back



     button .frame.file -text "File Manager" \

      -command { exec_cmd $cmds(file) } \

      -relief flat -padx 8 -bg $back



     button .frame.xv -text "Images" \

      -command { exec_cmd $cmds(xv) } \

      -relief flat -padx 8 -bg $back



     button .frame.xmah -text "Mahjongg" \

      -command { exec_cmd $cmds(xmah) } \

      -relief flat -padx 8 -bg $back



     # Pack all the buttons, in order.

     pack .frame.man .frame.mail \

       .frame.term .frame.file \

       .frame.xv .frame.xmah \

       -side left -fill y



     # Set up timer label.

     label .frame.time -bg $back

     update_time .frame.time

     pack .frame.time -side left -fill y



     pack .frame



     # toolbar.tcl

You can easily add your own commands, with three easy steps:

1.  At the set cmds area, add your new UNIX command.
2.  At the button area, copy one of the button commands to create your own. You need to change the button’s name to frame.yourname or something like that. Also, ensure that it uses your command from the set cmds area.
3.  At the pack command right after the button area, add the name of your new button.

You can use these two scripts as examples to get you started scripting Tcl applications. One of the handiest parts of Tcl is that it’s supported on Windows and Macintosh systems. There’s a number of books available on Tcl, including Graphical Applications with Tcl and Tk; see Appendix A for details.


Previous Table of Contents Next