Updated 2009-11-14 17:23:41 by dkf

Answered questions moved here from some Ask, and it shall be given. page. Probably in need of some wikignoming

Last line of output disappears edit

Why Does The Last Line Of Standard Output Sometimes Disappear?

(18/11/3) puts -nonewline

This is so simple it's embarrasing. I have a library routine
 proc warn {message} {
    puts "WARNING: $message"
    puts -nonewline "Continue? (y/N):"
    gets stdin a

...etc. The problem is that the "Continue" message does not appear until I press return...

RS stdout is flushed on newline - or explicitly by
 flush stdout

== or eq? edit

What's The Difference Between "==" And "Eq"?

MG April 22nd 2004 - Out of pure curiousity, what (if anything) are the pros and cons of using these different checks in an if {} statement?
  if { $str1 == $str2 } { # do something }
  if { $str1 eq $str2 } { # do something }
  if { [string equal $str1 $str2] } { # do something }

Peter Newman 26 April 2004: I have a Perl background; so have always assumed that "==" is for numbers and "eq" is for strings. So:-
 if { "01" == "1" } should return TRUE, and;
 if { "01" eq "1" ) should return FALSE.

But looking at the expr manpage, who knows? And I'm too lazy to fire up wish and check it out. But I always code on that assumption, and haven't yet encountered anything that suggests it's wrong. Also, I think "eq" is relatively new (Tcl 8.3 for example). So perhaps that explains the apparently redundant [string equal $str1 $str2]. But don't quote me on any of this.

MG Apr 28 2004 - I just checked that in wish, and it seems you're absolutely right. Thanks for the help :)

Resource command edit

I'm using the BatteriesIncluded Tcl/Tk for Jaguar, so this is Macintosh Tcl/Tk. According to the docs there's a resource keyword. However, I can't seem to use it. wish and tclsh don't seem to know anything about a "resource" keyword. I want access to the resource fork of files, if only simply to get a hold of the resource data in a block rather than as types and ids. How? BMA

Lars H: Under OS X the resource command is in the TclResource package, which was new in the 8.4.5 BI distribution. Try
  package require TclResource

Tcl commands at DOS prompt edit

Why Can't I Execute Tcl Commands From The DOS Prompt?

masCh: Hola. I downloaded the file lpttcl.zip, there's 2 files in it. The .sys file went into e:\windows\system32 and the .dll file is on my desktop. How do I run the program? I went to dos prompt and typed "load lpttcl" and all it said is "'Load' is not recognized as an internal or external command, operable program or batch file" Please help.

MG July 20th 2004 - Basically, the DOS prompt isn't what you want :) You need to be running the Tcl interpreter; there are two ways you can do that. (Assuming you're on MS Windows 95 or higher,) you can either launch 'tclsh' - either from the DOS prompt, or Start->Run and enter 'tclsh'. Then, with the prompt that comes up, use 'load lpttcl'. Or, you can run wish (which is, basically, tclsh but without the prompt, and with the Tk package loaded, so you can use a GUI). The best way to do that is to create a file called 'console.tcl', which has the single line:
 console show

and double-click it (when you install ActiveTcl, .tcl files are automatically associated with wish). Then you can enter commands into the console window that comes up.

Search using regular rexpressions edit

How Do I Search Strings Using Regular Expressions?

Adi: Hi. How can I use regular expressions in searching the index of a pattern in strings. e.g. something like:-
 string first {^fg[a-zA-Z]* $} tipfgt

where {^fg[a-zA-Z]* $} is a regular expression, and the string is tipfgt. Thanks

July 15 04 - MG Try this...
 set str tipfgt
 regexp {(fg[a-ZA-Z]*)} $str someVar
 string first $someVar $str

Lars H: Better yet, use the -indices option of regexp.

Start helper under Windows edit


Problem with command.exe /c start on Windows NT. I have a wish application that opens a ct data set and displays it. At the top of the main window I have a File menu with an option "new." When I execute this command I want a completely separate process to begin (exactly as if i started a different instance of the dos prompt and began my program again--using a wrapped wish interpreter with the Visualization Toolkit.) I don't want to use the -exec command (I don't think.) I have read so many different "how to" pages but have not been able to put the right combination of answers together. Help please! -kate

Peter Newman 30 June 2004: exec totally sucks on Windows. But the answer is the winutils shell and launch commands. These are so excellently brilliant it's just not funny. But whether they'll work on NT, I don't know (the docs don't say either way). But I think so.

MG - exec isn't so bad in my opinion, especially if you want/need to do it without any extensions for some reason. The code (in 8.4) would be this, I believe:
 set path [file attributes "c:/directory/your file.tcl" -shortname]
 eval exec [auto_execok start] $path &

Saving canvas contents edit

This question is neither basic nor about Tcl (but rather about Tk). Please move the answer if you spot a better page.

hi, in 02/19/2004 I asked for saving contents of canvas-widgets. follwing lines should help, but it doesn't:
 package require Img
 set im [image create photo -format window -data .canvas]
 $im write mycanvas.gif -format GIF

Interpreter says 'couldn't recognize image data'. Interpreters I checked out are wish82.exe and itkwish31.exe running under XP.
 package require base64
 package require Img

 canvas .cv
 pack .cv

 proc inlineGIF {img {name ""}} {
    set f [open $img]
    fconfigure $f -translation binary
    set data [base64::encode [read $f]]
    close $f
    if {[llength [info level 0]] == 2} {
        set name [file root [file tail $img]]
    set image [image create photo [list $name] -data {\n$data\n}]
    .cv create image 32 32 -image $image

 inlineGIF ./test.gif testgif

What is the problem? Thanks for all answers!

MG Apr 30th 2004 - As long as you have a window called .canvas, the first piece of code should work fine. At a glance, so should your second; if all you're doing with your $data variable is using it to create an image, though, why not simply use...
  canvas .cv
  pack .cv
  .cv create image 32 32 -image [image create photo testgif -file ./test.gif]

Data will produced in a canvas-widget. (e.g. barcode). Another application for printing labels only can handle .GIF-Files. So I need a possibility to create a GIF-Files from Canvas-Content to print it later ...

MGH Jan 23 2009 - I found a similar problem with trying to dump data from my .canvas, and traced it back to having the canvas as the child of a toplevel that was not ".". Moving the whole hierarchy back under the default top window ( . ) solved my problem.

Show variable value edit

on 17/08/2004 How can I see the value of some tcl environment varialbe at tcl prompt which I set through C program (using Tcl_Setvar)


17/8/04 MG - Looking at the man page for Tcl_SetVar, it looks as if, if you did...

In your C code, then you could (in your Tcl code) call the variable with either of
  set somevar

With '$somevar' and 'set somevar' being the equiv. of the C Tcl_GetVar() function, and 'set somevar value' the equiv. of the C Tcl_SetVar().