Reputation: 61
I am trying to get the value from ttk:entry
. I have the following code.
variable DefaultRoot
ttk::label $wi.custcfg.dlabel -text "Default Root:"
ttk::entry $wi.custcfg.daddr -width 10 -textvariable ::DefaultRoot -validate focusout -validatecommand { puts $::DefaultRoot; return 1}
puts $DefaultRoot
But I am getting error on the last puts
Upvotes: 1
Views: 1172
Reputation: 385910
The variable won't exist until you set it to some value. Merely declaring it as a variable (eg: variable DefaultRoot
) won't make it spring into existence.
With the code you posted, you're executing the last puts
about a microsecond after creating the entry widget. The user won't have the ability to enter any text before the puts
happens. Thus, the variable won't yet exist and the puts
will fail.
A simple solution is to make sure to set the variable before you call puts
, though that only means that the puts
will print the default value.
In other words, this will print "this is the default":
variable DefaultRoot
set DefaultRoot "this is the default"
ttk::entry $wi.custcfg.daddr -textvariable ::DefaultRoot
puts $DefaultRoot
To answer your specific question, however, you can use $::DefaultRoot
anywhere you want after the variable has been created.
For example, you could create a button that prints the value like this:
proc print_variable {} {
puts "DefaultRoot=$::DefaultRoot"
}
ttk::button $wi.custcfg.button -text foo -command print_variable
Upvotes: 2
Reputation: 30273
You can access the variable anywhere via
global DefaultRoot
puts $DefaultRoot
or
puts $::DefaultRoot
Upvotes: 1