0

In my codes, the entry cannot display the value. How to correct it? thanks

package require Itcl
namespace eval np {} {
  itcl::class myTable {
    variable tableValue
    constructor {} {
      array set tableValue {1 a 2 b 3 c 4 d}
    }

    proc build {} {
      destroy .e
      entry .e -textvariable [namespace current]::tableValue(1)
      pack .e
    }
  }
}
np::myTable tb
tb build
Jimmy
  • 135
  • 2
  • 10

1 Answers1

0

There are a few things that aren't quite correct. First the value is not [namespace current]::tableValue(1), this is the variable name. If you want the value, you'll have to use set in this case:

entry .e -textvariable [set [namespace current]::tableValue(1)]

But that's not quite there yet, because the above sets the text variable (a variable name that will hold the entry value) , and doesn't actually insert the value in the entry.

You probably want to use insert for that particular purpose:

proc build {} {
  destroy .e
  entry .e
  .e insert end [set [namespace current]::tableValue(1)]
  pack .e
}
Jerry
  • 70,495
  • 13
  • 100
  • 144
  • thanks for your info. But after the "-textvariable", we should specify the name of a global variable instead of value, right? And in your method, if i change the value in the entry, the value of the variable tableValue(1) won't change. – Jimmy Jan 28 '19 at 07:31
  • @Jimmy I don't quite understand the problem you are describing now. If you use `-textvariable` then use, you need to specify the name of a global variable. Wait I think I understand the misconception now; `-textvariable` sets the value of the variable name assigned *after* the entry box value is edited. It doesn't enter anything by itself into the entry box. – Jerry Jan 28 '19 at 10:55