itcl::scope

Scope gives a full description of an Itcl variable. Read the manual for details but this code (derived from the manual) may help:

  console show;  update idletasks

  package require Itcl
  itcl::class hellobtn { ;# a button restricted to the values 0-9
   private variable nuses 0
    constructor {frm} {
        puts "IN $this variable nuses is scoped [itcl::scope nuses]"
        pack [ button $frm.nus -textvariable [itcl::scope nuses] -command "$this increm 1" ]
    }
    method increm {dn} { incr nuses $dn; if {$nuses>9} { set nuses 0} }
  }
    # create a sample case:
  foreach ibtn {1 2 3 4 5 } {
      pack [frame .fi$ibtn] -side top
      foreach jbtn {1 2 3 4 5 } { ;# 25 buttons in total
        pack [frame .fi$ibtn.f$jbtn] -side left;   hellobtn  h$ibtn$jbtn .fi$ibtn.f$jbtn
     }
  } 

the output from the constructor is:

  IN ::h11 variable nuses is scoped @itcl ::h11 ::hellobtn::nuses

thus "scope nuses" has returned "@itcl ::h11 ::hellobtn::nuses" - or

  • we are in itcl, (@itcl indicates that there is a piece of code written in C for the itcl command - see the Itcl_RegisterC manual page)
  • the itcl object's global name is ::h11
  • the full name of the variable is ::hellobtn::nuses

In the next line, the text variable displayed on the button is a private variable (i.e., not a global as usually used by Tk) so multiple instances of hellobtn use the same variable name, but with the scope the variable becomes unique. The example creates 25 buttons using the same itcl class, all use the same internal variable name but each button increments its value independently of the other buttons. The class limits the range of the text variable to the range 0–9.