1

I am trying to run a procedure that takes an array called ds_out, changes values of ds_out(0) and ds(1) to either 0 or 1 depending on the state of a checkbox and returns it. I want to then output the values after the procedure but it looks like the procedure is not returning the array and printing the values in the initialising array. If you have the puts lines in the procedure it works.

I've looked at tutorials and examples but I don't understand them. I want the basic of basic examples but can't find them.

Below is the code I have used:

global ds_out
array set ds_out {
0   0
1   0
}

proc kl15cb {} {
checkbutton .kl15_cb -width 10 -height 1 -text "check me" -variable kl15_cb              -command {if {$kl15_cb} {
set ds_out(0) 0
set ds_out(1) 0
pack .kl15_cb
} else {
set ds_out(0) 1
set ds_out(1) 1
pack .kl15_cb
return [array get ds_out]
} } }

kl15cb
puts $ds_out(0)
puts $ds_out(1)
pack .kl15_cb
2
  • This is a problem of scope. Inside your proc, ds_out is a local array. You can use the global ds_out command to make the global array available. Alternatively you could use upvar, or create a new array when you call the proc. Commented Aug 16, 2018 at 11:20
  • @TrojanName: the name ds_out is never used in the procedure's local scope, only globally within the callback script. Commented Aug 16, 2018 at 15:22

1 Answer 1

2

No offense, but your code rests on some serious misconceptions. You are not dealing with a call stack here, where code can return values from one scope to a calling scope.

The code in the -command option is an event-oriented callback. It is not executed when the procedure is executed, just passed along to the checkbutton as a string. When the checkbutton is clicked, the callback is executed in the global scope, and it has nothing to return a value to.

Perhaps this snippet can help you along:

array set ds_out {
    0   0
    1   0
}

checkbutton .kl15_cb -width 10 -height 1 -text "check me" -variable kl15_cb -command {
    set ds_out(0) $kl15_cb
    set ds_out(1) $kl15_cb
}
pack .kl15_cb

If you run that code and then array get ds_out you will get 0 0 1 0. Now click on the checkbox and invoke array get ds_out again; now you get 0 1 1 1.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.