Computing: DOS, OS/2 & Windows Programming

Developing Tcl/Tk applications on Windows 11.

"Tcl (Tool Command Language) is a very powerful but easy to learn dynamic programming language, suitable for a very wide range of uses, including web and desktop applications, networking, administration, testing and many more. Open source and business-friendly, Tcl is a mature yet evolving language that is truly cross platform, easily deployed and highly extensible."

"Tk is a graphical user interface toolkit that takes developing desktop applications to a higher level than conventional approaches. Tk is the standard GUI not only for Tcl, but for many other dynamic languages, and can produce rich, native applications that run unchanged across Windows, Mac OS X, Linux and more. "

The two paragraph above are about how Tcl/Tk is described on the Tcl Developer Xchange website, where you find links to the downloads of the major Tcl/Tk distributions. This tutorial is about its installation and usage of Magicsplat Tcl/Tk for Windows (version 1.14.0, based on Tcl/Tk 8.6.14) on Windows 11; the tutorial should also apply to Windows 10.

You can download the software from the Magicsplat website. I chose the 64-bit installer (file: tcl-8.6.14-installer-1.14.0-x64.msi). Except for the license agreement, the installation proceeds without user intervention. The screenshot shows the last window of the setup program.

Installation of Magicsplat Tcl/Tk on Windows 11

The setup program will install several shells. Besides acting like consoles, these are interactive Tcl interpreters, i.e. if you enter some Tcl statement(s), it (they) will be executed, as shown on the screenshot below.

Executing some Tcl statements in the Magicsplat Tcl/Tk shell

Using TclTkIDE as IDE for Tcl/Tk development.

There are several IDEs available for Tcl/Tk, some written in Tcl/Tk themselves, some others being general purpose IDEs that are configurable for Tcl/Tk. I tested TclTkIDE, a Tcl/Tk IDE for Windows, that the author of the software himself calls "a hobby project with no warranty". Essentially, it includes all that you need to play around with Tcl/Tk: an editor with syntax highlighting, syntax checking, procedure listing, regexp search function, possibility to run Tcl/Tk scripts (by simply pushing the Run and Stop buttons). The IDE also includes a feature to wrap Tcl/Tk scripts into Windows EXE files. This functionality seems however only succeed under given conditions (?); for the script listed at the end of this tutorial, I got an error in startup script message and no executable was produced (?).

You can download the IDE (version 0.9 from 2016) from github. The download archive contains the files shown on the screenshot below, among them makeTclTkIDE.BAT, that will be used to create the IDE executable. In fact, the IDE itself is written in Tcl/Tk, so we must create an executable corresponding to the Tcl/Tk version that we actually use. With Tcl/Tk 8.6.14 installed, the batch file has to be run as follows:
    makeTclTkIDE 86
This will create TclTkIDE86.EXE, the Windows executable of the IDE.

Creating the TclTkIDE executable for Tcl/Tk 8.6

I copied the files from the temporary folder to C:\Programs\TclTkIDE. You can start the application from here (this might result in a warning from Windows and the need to confirm that you want to run this program), or better create a shortcut on the desktop or in the Start menu. The screenshot below shows the IDE with an empty editor window, as it looks when just having started it.

Using TclTkIDE for Tcl/Tk development on Windows 11

The screenshots below show a simple Tk application. An edit field, where the user can enter their name and a button. Pushing the button will replace the user name by a personal greeting message. Look at the code that is really simple (the instruction pack packs the GUI components one beneath the other). To start the script, push the green Run button, to stop it, push Stop.

Running a Tk script in TclTkIDE [1]
Running a Tk script in TclTkIDE [2]

To terminate this tutorial, a bit of Tcl/Tk code, so you can see what it looks like and what you can do with this powerful script language. The example listed here is a somewhat special implementation of Pascal's triangle. It is in fact a one-dimensional cellular automaton with a few very simple rules. The code of this program, written by Markus Arjen, has been published at tcl-lang.org.

    # Turn Pascal's triangle into a cellular automaton
    #
    # RS's original:
    #

    proc pascal {{lastrow ""}} {
        set res 1
        foreach a [lrange $lastrow 1 end] b $lastrow {
            lappend res [expr $a+$b]
        }
        set res
    }
    #
    # AM's modulo version
    #

    proc pascalam {mod {lastrow ""}} {
        set res 1
        foreach a [lrange $lastrow 1 end] b $lastrow {
            lappend res [expr ($a+$b)%$mod]
        }
        set res
    }
    #
    # Translate the result into colours
    #

    proc tocolours {row} {
        set rowcols {}
        foreach a $row {
            lappend rowcols [lindex {white black blue green yellow orange red} $a]
        }
        set rowcols
    }
    #
    # Show a row
    #

    proc showrow {rowcols xc yc} {
        global size
        set xoff [expr {$xc-(([llength $rowcols]+1)/2)*$size}]
        set x1   $xoff
        set y1   $yc
        set y2   [expr {$yc+$size-1}]
        foreach a $rowcols {
            set x2 [expr {$x1+$size-1}]
            .c create rectangle $x1 $y1 $x2 $y2 -fill $a -outline $a
            set x1 [expr {$x1+$size}]
        }
    }
    #
    # Create the canvas and show the rows
    #

    pack [canvas .c -bg white -width 400 -height 400] -fill both
    set row  1
    set mod  5
    set size 2
    set xc   200
    set yc   0
    for { set i 0 } { $i < [expr {400/$size}] } { incr i } {
        showrow [tocolours [set row [pascalam $mod $row]]] $xc $yc
        incr yc $size
    }

And here is the output of the script running in TclTkIDE.

Pascal's triangle, by Markus Arjen, running in TclTkIDE

If you find this text helpful, please, support me and this website by signing my guestbook.