June 2005

A Simple tck/tk Menu

One problem faced by many X windows users is when switching window managers and/or environments, access to applications may vary. Many full environments have the ability to automatically configure where applications reside. Not all do, however. One simple solution to that problem is to use the wish interpreter to whip up a quick tcl/tk menu that should run under any window manager and/or environment.

Note the text is not a tcl/tk tutorial nor is it a full blown wish tutorial.

Prerequisites

In order for it to work, obviously tcl/tk and wish need to be installed on the system. They can be downloaded at the tcl/tk web site.

Additionally, to make sure under any window manager that the menu can be started it is a good idea to use a $HOME/.xinitrc file that starts an xterm before the window manager. Following is a simple example:

xterm &
wmaker

Once the menu is completed, it can be added to the .xinitrc file as well.

A Bare Bones

A copy of all the code used in the text can be found at the end of it.

The first draft contains the title of the menu which will appear in the window title, one command and an exit call.

#!/usr/bin/env wish

wm title . "My Menu" 

button .xterm -text "xterm" -bd 1 -foreground #444444 -command \
        { exec /usr/X11R6/bin/xterm -ls & } -width 8 

button .exit -text "exit" -bd 1 -foreground #444444 -command \
        {exit} -width 8 

pack .xterm .exit

Now a line by line:

#!/usr/bin/env wish

Sets up the call to the wish interpreter.

wm title . "My Menu"

Is the line that sets the title of the window.

button .xterm -text "xterm" -bd 1 -foreground #444444 -command \
	{ exec /usr/X11R6/bin/xterm -ls & } -width 8

The first button, the .xterm is what the button is called with regard to the script - not the label on the button itself. For simplicity - they are the same. Next is -text "xterm" which is what shows up on the button itself. The -bd is the border depth and -foreground is the color of the text. Next comes the -command where in brackets, the literal command can be put as well as an & to make sure it detaches. Next is another command, exit, which follows a similar format. The -width in pixels. Last and not least, the pack directive places the buttons in order. Since the script does not define the precise placement of the buttons, they simply follow one after the other like so:

image

Dealing with fonts

The default font for the menu may vary, so in order to use a particular one, the font can be specified within the script itself. Following is an example with helvetica:

font create helvfont -family Helvetica -size 10 -weight normal \
 -slant roman wm title . "My Menu"

Everything in that syntax is pretty self explanatory except the font create, that is used to give the font a title which is used internally by the script. To use the font for an item, just add the -font <name> flag to each item. Following is the new script with the font defined for each element:

#!/usr/bin/env wish

font create helvfont -family Helvetica -size 10 -weight normal -slant roman
wm title . "My Menu"

button .xterm -text "xterm" -bd 1 -foreground #444444 -command \
    { exec /usr/X11R6/bin/xterm -ls & } -width 8 -font helvfont

button .xlock -text "xlock" -bd 1 -foreground #444444 -command \
    { exec xlock -mode blank & } -width 8 -font helvfont

button .exit -text "exit" -bd 1 -foreground #444444 -command \
    {exit} -width 8 -font helvfont

pack .xterm .xlock .exit

The new menu with a defined font looks like so:

image

Adding Items

Now that the basic look and feel is finished, it is simple enough to add a few key items that are commonly used from desktop to desktop, following is the code for the finished version:

#!/usr/bin/env wish

font create helvfont -family Helvetica -size 10 -weight normal -slant roman
wm title . "My Menu"

button .xterm -text "xterm" -bd 1 -foreground #444444 -command \
    { exec /usr/X11R6/bin/xterm -ls & } -width 8 -font helvfont

button .xlock -text "xlock" -bd 1 -foreground #444444 -command \
    { exec xlock -mode blank & } -width 8 -font helvfont

button .firefox -text "firefox" -bd 1 -foreground #444444 -command \
    { exec /home/mui/firefox/firefox & } -width 8 -font helvfont

button .soffice -text "soffice" -bd 1 -foreground #444444 -command \
    { exec /home/mui/soffice/program/soffice & } -width 8 -font helvfont

button .exit -text "exit" -bd 1 -foreground #444444 -command \
    {exit} -width 8 -font helvfont

pack .xterm .firefox .soffice .xlock .exit

and a pic of what it looks like:

image

Source Code

A copy of all the source code examples is available:

Summary

The tcl/tk language and kit are very useful. Applying the wish interpreter to get quick - and good - results can help in creating simple portable X windows guis or just plain old application launchers.

 

Digg!
Submit site
news to Digg!

Slashdot Slashdot It!
Delicious Bookmark on Delicious