Version 0 of Restarting a tkcon application

Updated 2001-01-04 09:17:37

[email protected] wrote in the comp.lang.tcl newsgroup:

My solution is a procedure I call "restart" which does its best to restore a wish--tkcon actually--session back to its original state. It deletes child windows, resets images, fonts, bindings, after events etc. And if you give it a file name, it will source that file.

So I run tkcon and emacs, and after making a change to the source file in emacs, I type 'restart myfile.tcl' in tkcon.

So you need to add the following to your .wishrc file:

 foreach p [info procs] { set kpv(p,$p) 1 }
 foreach p [info vars]  { set kpv(v,$p) 1 }
 set kpv(p,bgerror) 1

 ####################################################################
 #
 # Restart
 #
 # Deletes all non-root widgets and all bindings. This allows us to
 # reload the source file without getting any errors.
 #
 proc restart {{f ""}} {
    global kpv tk_version

    ;#catch {auto_reset}                  ;# Undo all autoload stuff
    if [info exists tk_version] {         ;# In wish not tclsh
        . config -cursor {}
        eval destroy [winfo children .]   ;# Kill all children windows

        foreach v [bind .] {              ;# Remove all bindings also
            bind . $v {}
        }
        if {$tk_version >= 4.0} {               ;# Kill after events
            catch {foreach v [after info] {
                after cancel $v
            }}
        }
        if {$tk_version >= 8} {                 ;# Font stuff
            catch { eval font delete [font names] }
            eval image delete [image names]     ;# Image stuff
        }
        wm geom . {}                     ;# Reset main window geometry
        . config -width 200 -height 200
    }
    foreach v [info procs] {               ;# Remove unwanted procs
        if {[info exists kpv(p,$v)] == 0} {
            catch {rename $v {}}
        }
    }
    uplevel {                             ;# Get at global variables 
        foreach _v [info vars] {          ;# Remove unwanted variables
            if {[info exists kpv(v,$_v)] == 0} {;# Part of the core?
                catch {unset $_v}
            }
        }
        catch { unset _v }
    }
    if [file exists $f] {
        uplevel source $f
    }
 }

Arts and crafts of Tcl-Tk programming