Re: Some newbie questions
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.tcl archive

Re: Some newbie questions

From: Michael A. Cleverly <michael@cleverly.com>
Date: Sat Jul 30 2005 - 18:34:23 CEST

On Sat, 30 Jul 2005, it was written:

> OK, but what if my script is in a file? source the file(s) again doesn't work
> if the script contains commands that create widgets. In other words, if in
> your example the proc 'abc' would contain as an example the command 'button
> .b -text "Hello"', and then change the command to 'button .b -text "Bye"'
> re-executing the proc, would give an error: "a window named .b already
> exists".

You can resource your application while it is running. With a little bit
of care, that is.

First, you'll want to save where your application is run being run from.
To do this, take a look at [info script] (which returns the name of the
file currently being sourced) as well as [pwd] and [file join] (if your
application might have changed directories between when it started and
when you want to resource it).

     set ::filename [file join [pwd] [info script]]

Then later, you can:

     source $::filename

to reload any changes.

You just need to take care to not recreate widgets (or other things that
you can realistically only do the first time you're loading your
application). If you set ::filename as the last thing before dropping
into the event loop then earlier in your code you could write (as per
your example):

     if {![info exists ::filename]} then {
         button .b -text "Hello"
     } else {
         button .b -text "Bye"
     }

or you could separate widget creation from widget configuration and write:

     if {![info exists ::filename]} then {
         button .b
     }

     .b configure -text "Hello"

then you could change your source file and edit the above line to read:

     .b configure -text "Bye"

and reload it.

Michael
Received on Thu Sep 29 14:28:03 2005