I have a tcl/tk based application where code is invoked using .bat
files. say there is a command abc.bat which can invoke wish abc.tcl
and there is another command abc_1.bat which invokes tclsh abc.tcl.
Now in abc.tcl I want to handle some errors and display them to user
appropriately like when in Tk mode I wish to use messagebox and when
in "tclsh" mode" its ok to use puts.
Is there any way of knowing it runtime that whether wish or tclsh was
used to invoke this application??
Thanks,
Deepanshu
One way: use [package present Tk]
You either get the version number or a string that it
is not available.
Regards,
Arjen
Sure. There is more than one way to do it. But basically you don't
care about tclsh or wish, you care about Tk being present...
So the sane way is to check for Tk...
(bin) 50 % if {[catch {package present Tk}]} {
puts "No Tk"
} else {
puts "Have Tk"
}
No Tk
(bin) 51 % package require Tk
8.4
(bin) 52 % if {[catch {package present Tk}]} {
puts "No Tk"
} else {
puts "Have Tk"
}
Have Tk
Michael
There are several ways to do this depending on exactly what you mean.
If you REALLY need to find out if you're in tclsh or wish then you can
check [info nameofexecutable]:
set app [info nameofexecutable]
if {[regexp {wish} $app]} {
pack [label .l -text "This is running in wish."]
} elseif {[regexp {tclsh} $app]} {
puts "This is running in tclsh."
} else {
puts "Whoa... we're in some weird custom app!"
}
On the other hand, usually you only need to know if this is a GUI
environment or a command line environment. In which case I usually
find it more robust to check if Tk is loaded:
if {[info commands winfo] == ""} {
puts "We don't have GUI, this is probably tclsh."
} else {
pack [label .l -text "We have GUI capabilities."
}
> One way: use [package present Tk]
> You either get the version number or a string that it
> is not available.
In my opinion: "...or a remains unset" (set a [package present Tk]).
--
ZB