Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

opening a tcl file from another ?

Status
Not open for further replies.

zskater

Programmer
Apr 14, 2001
22
GB
If i have 2 files eg. test1.tcl and test2.tcl
and i want a button in test1 to run test2.tcl
what do i need to write in test1.tcl

I have been trying -command {source test2.tcl}

but always get the message
could not read file "test2.tcl" : no such file or directory

test1.tcl and test2.tcl are both in the same folder as each other

I also tried it at uni on the unix computers and it crashed the X server

hope someone can help
Cheers
Steve
 
I think the "source" command should work. Have you tried fully qualifying the path to "test2.tcl"? Bob Rashkin
rrashkin@csc.com
 
Remember that the source command instructs the Tcl interpreter to read and execute the file you name as part of the same process that's currently executing. It does not start another program running in parallel with the one currently executing.

The only reasons I can think of for source not being able to read the file are:
[ul][li]Your script executed a cd command at some point to change its current working directory. You can test this by having your script print the return value of the Tcl pwd command. If this is the problem, either change back to your original directory, or use an absolute or relative pathname when you source the script.[/li]
[li]You have some sort of file permissions problem. Check to make sure that you have read permission on the files you're sourcing.[/li]
[li]You misspelled the name of the file you're sourcing.[/li][/ul]

I'm particularly concerned about the crashed X server. In general, Tcl shouldn't do things like that. And it's very difficult to diagnose a problem like that without a lot more information about your scripts. There's one potential cause I can think of. (And it relates to the idea that you might really be wanting to exec your script instead of sourcing it. More about that below.) If you use both the grid command and the pack command to manage widgets within a single parent (either a frame or a toplevel window), the contention between the geometry managers has been known to crash older X servers. Your guidelines for using pack and grid is to use only one geometry manager within a single manager widget. You can use a different geometry manager within a descendant widget (for example, you can grid the contents of a frame, and then pack the frame into a toplevel), but you can't mix both at the same level.

But getting back to the idea that source might not be the command you really want. If you want to have your script execute another program in parallel with a script already executing, you want to use the Tcl exec command. Give the exec the name of the program to execute (providing an absolute path, if the program isn't in one of the directories listed in your system's PATH environment variable) along with any arguments you want to pass to the program.

Keep in mind that if you want your script to start another Tcl script, you actually want to execute the tclsh (or wish) interpreter, passing the name of the script as a command-line argument. Thus, for test1.tcl to execute test2.tcl, the command to use in test1.tcl would be:

Code:
exec tclsh test2.tcl

Note that the exec command doesn't return until the program executed terminates. The return value of exec is any output generated by the program.

If the program you execute takes a long time to run (or runs until explicitly killed), you might want to tell exec to start the program running in the background, and to then return immediately. To do so, use "&" as the last argument to exec. For example:

[tt]exec tclsh test2.tcl &[/tt]

The downside to this technique is that any output produced by the program is discarded. You can instruct exec to redirect the output to a file, for example:

[tt]exec tclsh test2.tcl >output.txt &[/tt]

But overall, this is a simplistic technique. Tcl has more elegant interprocess communication mechanisms, but that would require a much longer reply to describe.

- Ken Jones, President
Avia Training and Consulting
866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
i have now been working on the project non stop for 12hours
today, started at 7am this morning.

I think i will wait till tomorrow before trying out your suggestions

Thanks very much
I shall let you know how i get on
Cheers
Steve

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top