Jump to content

Tcl: Extract variables from checkbuttons into Tcl List

- - - - -

  • Please log in to reply
No replies to this topic

#1
WhatFurrer

WhatFurrer

    Newbie

  • Members
  • Pip
  • 1 posts
I am attempting to create a GUI which displays a list of checkboxes which describe a list of tests that may be selected to run on a device. Since I am very new to Tcl, I found an example program that has the basics that I need and am attempting to modify it to do what I need.

Below is a copy of what I am working from:

#!/usr/bin/wish

# ckbutton.tcl

# Demonstrate the checkbutton command


# Perform the specified action on one or more checkbutton widgets

proc DoButton {action ckbuttons} {

	foreach ckbutton $ckbuttons {

		switch $action \

			toggle { $ckbutton toggle } \

			clear { $ckbutton deselect } \

			set { $ckbutton select }

	}

}


# Show the status of the specified widget's variable

proc ShowStatus {ckbuttons} {

	foreach ckbutton $ckbuttons {

		set var [$ckbutton cget -variable]

		global $var

		set val [set $var]

		puts "$var = $val"

	}

}  


#for {set i 0} {$i < [llength $ckbuttons]} {incr i} {

#    puts "i = $i"

proc Add2RunList {ckbuttons} {

	foreach testbutton $ckbuttons {

		set var [$testbutton cget -variable]

		global $var

		set val [set $var]

		lappend runlist "$var = $val"

	}

} 


 

proc DisplayRunlist {runlist} {

	puts "$runlist (length: [llength $runlist])"

	}


# Checkbuttons

set ckTest1 [checkbutton .cktest1 -text "Test 1 Checkbutton" \

	-offvalue "OFF" -onvalue "ON" -command {Add2RunList .cktest1}]

set ckTest2 [checkbutton .cktest2 -text "Test 2 Checkbutton" \

	-offvalue "OFF" -onvalue "ON" -command {Add2RunList .cktest2}]

set ckTest3 [checkbutton .cktest3 -text "Test 3 Checkbutton" \

	-offvalue "OFF" -onvalue "ON" -command {Add2RunList .cktest3}]


# Create a list of checkbuttons

set ckbuttons [list $ckTest1 $ckTest2 $ckTest3]


set runlist [list 1 2 3 4]

global $runlist


# Buttons

set bExit [button .eb -width 8 -text "Exit" \

	-command exit]

set bToggle [button .toggle -width 8 -text "Toggle All" \

	-command {DoButton toggle $ckbuttons}]

set bClear [button .clear -width 8 -text "Clear All" \

	-command {DoButton clear $ckbuttons}]

set bSet [button .set -width 8 -text "Set All" \

	-command {DoButton set $ckbuttons}]

set bShow [button .show -width 8 -text "Show All" \

	-command {ShowStatus $ckbuttons}]

set bDisplay [button .display -width 12 -text "Display Runlist" \

	-command {DisplayRunlist $runlist}]


#puts "$runlist (length: [llength $runlist])"


# Display

pack $ckTest1 $ckTest2 $ckTest3 -anchor w

pack $bToggle -pady 5 -padx 5 -side left

pack $bClear $bSet $bShow $bDisplay $bExit -pady 5 -padx {0 5} -side left


#puts "$ckbuttons (length: [llength $ckbuttons])"

#puts "$runlist (length: [llength $runlist])"

I have created a partially populated list called "runlist" that has the elements 1,2,3,4 (just placeholders for now) in it. I have created the checkbuttons with the command of "Add2RunList". My issue is that when any checkbutton is "checked" and "Add2RunList" executes, it does not append anything to the end of "runlist". I created a procedure to display "runlist" but regardless when I run it, "runlist" continues to have only the original elements that I installed in the list upon its creation. I can append elements to the list "outside" of the procedure but not inside...probably some sort of a "scope" problem, but I am at a loss as to how to correct it.

I have tried searching for a solution or mention of a partial solution on-line but mostly what I find is copies of the same man page for "checkbutton".

Would someone with Tcl experience please review the above script and make suggestions as to a possible solution? I have been working on this for two days now and continue to make no headway.

Any assistance would be greatly appreciated.

Thanks,

WhatFurrer




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users