Version 17 of tclsh vs. wish

Updated 2003-07-17 15:51:21

Differences include:

  • event loop
  • various memory usage patterns
  • GUI service

Under DOS,

    tclsh myscript.tcl > output.txt

does what one wants, but it's harder to achieve with wish.

Luciano ES Harder? Harder how? (please read on...)


This topic was raised at comp.lang.tcl by Luciano ES who asked, boldly: "What do we need tclsh for?" My point was that wish can do everything that tclsh can, plus the extra optional features, so why not ditch tclsh and keep wish only?

Among the several mentioned reasons, were:

  • tclsh does not require a graphics server, like X. Also, tclsh uses a lot less resources than wish;
  • many applications that rely on Tcl/Tk are run on very low-spec hardware;
  • many (most?) applications do not need a GUI at all;
  • under Windows, tclsh has a console associated with it and it's the only way to see the various printf messages your code has under Windows;
  • Tcl makes very frequent use of event loops, which under Tk results in a probably unwelcome blank window being shown;
  • after processing a script, tclsh exits, while wish hangs around, entering the event loop;
  • LV adds: The very act of starting a wish begins by attempting to create and display a toplevel widget. For historical reasons, Tk has always created and instantiated a toplevel.

It was also noted that the opposite is a lot easier to achieve, i.e. ditch wish and keep tclsh.

 package require Tk 8.4

The simple line above in a Tcl script, even if run by tclsh, will immediately make all Tk functionality available throughout the rest of the program. A little (just a little) more about that is discussed in Using Tk as a loadable package.


Someone also added that: "At a DOS prompt I can say...

 tclsh myscript.tcl >output.txt

... to capture the output (of practically unlimited size). Using wish for this kind of stuff might be possible but would be more involved IMO."

The information was confirmed by a notorious Tcl expert, who also said that "Tkcon would require an exec at the beginning of that command"

But it still puzzles me:

 wm withdraw .
 puts "hi, Mom"
 exit

The script above can be run by wish and produce the same result as tclsh.

  wish myscript.tcl >output.txt

in a DOS prompt produces just the same result as...

 tclsh myscript.tcl >output.txt

I also ran wish myscript.tcl >output.txt from Tkcon, without the exec command, and had exactly the same result (shrug).

So, my conclusions so far are:

  • if I use wish instead of tclsh, I have to use wm withdraw . if I don't want any windows, and exit because wish won't exit automatically like tclsh;
  • wish uses up more resources than tclsh, and that is often not good;
  • if there is no $DISPLAY environment variable available, wish refuses to initialize, but that is hard-coded and I wonder if it could be changed. [ CL notes that "Image manipulation without Tk", which now appears just confusing, has fragments of an explanation that bears on this topic.]

Note that I don't really want to get rid of tclsh. It is all just for the sake of getting a better grasp of the differences between the two programs.


SLB From a DOS prompt try running

  wish myscript.tcl

i.e. with no redirection. The output will be lost. Conversely, if you setup file extensions so you can run Tcl scripts from Windows Explorer via tclsh, you will get you will get a DOS Window.


category internals