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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

One-time Startup Execution

Status
Not open for further replies.

cptk

Technical User
Mar 18, 2003
305
US
I need to execute a command only one time (during startup).
The cmd opens a text file.

> set FID [open some_text_file.txt r]

Two things:
1.) How do I only run this only once at startup? I tried binding to an arbitrary widget, but this causes it to be rexecuted every time the gui is brought back in focus - not desired! If bind is the right way, what type of bind should I use? example ..
> bind .exit <Map> Startup_script

2.) I can't seem to use the FID globally. Why?
 
Try something like this.

proc initfile {fname} {
global fid
if {[file exists $fname]} {
set fid [open $fname]
return [expr [file size $fname] / 1024]
}
}
 
Thanks for the reply , but ...
This doesn't resolve my problem. I want to be able to re-read the channelid in another proc; I'm assumming I can do that. When I run the [eof $FID], it always returns &quot;1&quot;.
Note also that I never close the the file.

What about the bind question regarding startup?
 
Found what I need for question #2.
use the seek cmd.
> seek $FID 0 start

 
Ah.. I get you now..sorry.

Use trace.
proc reread {x y z} {
global fid
if {[eof $fid]} {
seek $fid 0 start
}
return [set fid]
}

#main()
set fid [open &quot;/myfilename&quot;]
trace add variable fid read {reread}
etc...

I haven't programmed extensively with tk: bong,
ulis or avia are better for that.

HTH.
 
Am I correct in my interpretation that all you want to do is open a file for reading as part of the initialization of a GUI program? In that case, you don't need to bind the action to anything. (In fact trying to do so makes is much more complicated that it needs to be.) Simply open then file as part of the mainline intialization; don't include it in any bindings. For example:

Code:
proc Refresh {} {
    global fid
    .t delete 1.0 end
    seek $fid 0 start
    .t insert end [read $fid]
}

set fid [open myfile.txt r]

text .t
button .b -text &quot;Refresh&quot; -command {Refresh}

pack .t -expand yes -fill both
pack .b -pady 4
- Ken Jones, President, ken@avia-training.com
Avia Training and Consulting, 866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
thanks ken & marsd ...

Ken,
I think I need abandon specTcl (gui app.) because when
I place the following cmd outside a proc,
> set fid [open myfile.txt r] ,
the channelid $fid is NOT initialized when I try to access later on in a proc. However, when I create a stand-alone wish script, it works fine. This is the crux of my original problem (#1 bove) when I was asking about startup steps. It was my experience that I couldn't place certain cmds in a no proc environment. That doesn't seem to be the case. YOU CAN PUT ANYTHING outside a proc which will then run only one time ... RIGHT? ... please say yes!

 
That's right. Anything outside of a procedure should be executed only once. The Tcl interpreter is simply reading and executing its way down your script. Once it reaches the bottom, the tclsh interpreter exits whereas the wish interpreter drops into the event loop.

I've not used specTcl myself. (I have a person bias against GUI builders, as they never generate code that I would personally write, and I find them far too sensitive about changes that I can make to the generated code.) So, I can't comment on how specTcl might structure the generated code. Perhaps -- and I'm only speculating here -- it might be putting some code into a &quot;main&quot; procedure that it creates and then explicitly invokes near the bottom of the script. That might cause it to be sensitive to the type of code that you can add and where you can add it. But like I said, I'm only speculating here... - Ken Jones, President, ken@avia-training.com
Avia Training and Consulting, 866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Ken --
Can execute a defined procedure OUTSIDE a procedure?
 
Sure. Once you define a procedure, you can use it anywhere you could use a built-in Tcl command. The only restriction is that you must define the procedure before you can execute it. For example:

Code:
proc init {} {
    # Define &quot;fid&quot; as being in the global scope
    global fid
    set fid [open myfile.txt r]
}

# Later on...

init

What causes confusion for some people is code like this:

Code:
button .b -text &quot;Click me&quot; -command ClickIt
pack .b

proc ClickIt {} {
    puts &quot;Thanks for clicking!&quot;
}

This is actually legal and functional Tcl code. The first line doesn't actually execute the ClickIt procedure. It simply tells the button that when a user clicks the button, it should execute the ClickIt procedure. Because the button isn't even displayed until we reach the event loop, the proc defining ClickIt gets a chance to execute before the user has a chance to click the button. However, as a matter of programming style, I prefer to have all of my procedure defintions at the beginning of an application (or in separate files that get sourced in at the beginning of an application). - Ken Jones, President, ken@avia-training.com
Avia Training and Consulting, 866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top