Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with Bindings

Status
Not open for further replies.

fabien

Technical User
Sep 25, 2001
299
AU
Hi,

I have the following problem, I have a variable scriptselect I declared global: scriptsel. It is linked to a label via

label .inputframe.label2 -text $scriptsel ..

later in the program I create a seperate window via a proc to select an item from a list

and I used bind $w.frame.list <Double-1> {set scriptsel [selection get] }

I declared scriptsel global in this function as well but the scriptsel in the label is not updated. Why is that?


I have cut and paste the whole program below:
#!/usr/local/bin/wish
#-----------------------------------------------------------------------------
#
# set paths
set scriptspath &quot;/export/home/fabien/Cshscripts/&quot;
set pathlength [string length $scriptspath]
global pathlength

# script selected
set scriptsel &quot;<NONE SELECTED YET>&quot;
global scriptsel

# global list
global slist

# main window
#
wm title . &quot;AWK&quot;

# browse(dir) is the directory containing all the AWK files
set browse(dir) $scriptspath
# set file containing the scripts
set menufile [append scriptspath &quot;MENU&quot;]

global browse

#general Menu
menu .menuBar -tearoff 0
set m .menuBar.file
.menuBar add cascade -menu $m -label &quot;File&quot; -underline 0
menu $m -tearoff 0
$m add command -label &quot;Select Script&quot; -command {Selectscript} -underline 0
$m add separator
$m add command -label &quot;Exit&quot; -underline 1 -command {exit} -accelerator &quot;Meta-Q&quot;

global m sub

. configure -menu .menuBar

#
# Top frame, holds buttons, command
#
frame .inputframe -borderwidth 10
frame .buttonframe -borderwidth 10
pack .inputframe .buttonframe -fill x

#
# Action Buttons
#
set but [button .buttonframe.run -background #88ff88 -activebackground #88ff88 -text &quot;Run&quot; -command Run]
button .buttonframe.quit -background #ffbbbb -activebackground #ffbbbb -text &quot;Quit/Cancel&quot; -command exit
button .buttonframe.help -text &quot;Help&quot; -command Help -background #ffffbb -activebackground #ffffbb
pack .buttonframe.run .buttonframe.help .buttonframe.quit -padx 12 -side left -fill x

#
# Input data
#
label .inputframe.label1 -text &quot;Script selected: &quot; -padx 0
label .inputframe.label2 -text $scriptsel -padx 0
pack .inputframe.label1 -side left
pack .inputframe.label2 -side left

#
# Lower frame holds text output
#
frame .t
set log [text .t.log -width 80 -height 15 -borderwidth 3 -relief sunken -setgrid true -yscrollcommand {.t.scroll set}]
scrollbar .t.scroll -command {.t.log yview}
pack .t.scroll -side right -fill y
pack .t.log -side left -fill both -expand true
pack .t -side top -fill both -expand true


#
# Functions
#
# Run command
#
# Run the program and arrange to read its input
proc Run {} {
global script inputfile outfilename log but
# set script $binpath/volmath
# test ##########
set script &quot;xterm -e nawk -f decposlog&quot;
set inputfile poslog.dat
###########

if {[winfo exists .msg]} {destroy .msg}

# check that all entries have been made
if {[string length $inputfile] == 0} {
message .msg -background white -foreground red -width 500 -text &quot;Error: Input data is required&quot;
pack .msg -fill x -expand true
return
}
# running nawk script/Log
append script &quot; $inputfile &quot;
$log insert end &quot;Command line:\n&quot;
$log insert end $script\n
catch { puts $script }
if [catch { open &quot;|$script |& cat&quot; } input ] {
$log insert end $input\n
} else {
fileevent $inputfile readable Log
$log insert end $input\n
$but config -text Stop -background #ff2222 -activebackground #ff2222 -command Stop
}
}
# Stop the program and fix up the button
proc Stop {} {
global input but
catch {close $input}
$but config -text &quot;Run it&quot; -background #88ff88 -activebackground #44ff44 -command Run
}


# Read file containing the menus and then create the menus
proc Readfilemenu {} {
global browse menufile m sub pathlength slist
#loop
set i 0

if {[winfo exists .msg]} {destroy .msg}

AddText &quot;Reading File $menufile, please wait...\n&quot;

# test if can open themenu file
if { [file exists $menufile] } {
if { ![file readable $menufile] } {
# cannot find or open the file
tk_messageBox -title &quot;Error: $menufile&quot; -message &quot;File cannot be found or read\n, please run xxx to create it&quot; -type ok
exit
} else {
# the file exists read the data from it
AddText &quot;Creating the menus..\n&quot;
set c 0
set fd [open $menufile &quot;r&quot;]
set data [read $fd]
# process data file
set data [split $data &quot;\n&quot;]
foreach line $data {
puts $line
# strip off path from name and store it in array
lappend slist [string range $line [expr $pathlength ] end]
AddText $data\n

}
AddText &quot;Done!\n&quot;
}

}

}


# add text into log window
proc AddText {string} {
global log
$log insert end $string
update
}


#
# create the script select window list
proc Selectscript {} {
global slist scriptsel

set font {Helvetica 14}
set w .script
catch {destroy $w}
toplevel $w
wm title $w &quot;Select Script&quot;
wm iconname $w &quot;Scripts&quot;

#label $w.msg -font $font -wraplength 4i -justify left -text &quot;Scan the list either using the #scrollbar or by dragging in the listbox window with button 2 pressed. Double-click on button 1 to #select the script to be run&quot;
#pack $w.msg -side top


frame $w.buttons
pack $w.buttons -side bottom -fill x -pady 2m
button $w.buttons.dismiss -text Dismiss -command &quot;destroy $w&quot;
pack $w.buttons.dismiss -side left -expand 1

frame $w.frame -borderwidth 10
pack $w.frame -side top -expand yes -fill y


scrollbar $w.frame.scroll -command &quot;$w.frame.list yview&quot;
listbox $w.frame.list -yscroll &quot;$w.frame.scroll set&quot; -width 20 -height 16 -setgrid 1
pack $w.frame.list $w.frame.scroll -side left -fill y -expand 1
bind $w.frame.list <Double-1> {
set scriptsel [selection get]
puts &quot;scriptsel: $scriptsel&quot;
}

# loop through the list
foreach item $slist {

$w.frame.list insert end [lindex $item 0]

}

}

#
# help window
#
proc Help {} {
if { [winfo exists .help] } {
raise .help
} else {
toplevel .help
wm title .help &quot; help&quot;
#
button .help.quit -text &quot;Quit/Dismiss&quot; -command &quot;destroy .help&quot;
#
# Lower frame holds text output
#
set tt [text .help.text -width 80 -height 6 -borderwidth 2 -relief sunken -setgrid true -background white -wrap word]
$tt insert end &quot;blblsaldsdlsdsd.&quot;
#
pack .help.text -side top -fill both -expand true
pack .help.quit -side bottom
}
}

# Read file containing the script names and create submenus
Readfilemenu


Thanks,

Fabien
 
It doesn't work because the command:

Code:
label .inputframe.label2 -text $scriptsel

doesn't link the variable to the label.

When figuring out how Tcl scripts work (and don't), you need to think like a Tcl interpreter. And, to simplify things a little, the Tcl interpreter thinks this way when evaluating a command:
[ol][li]First group the arguments based on whitespace separatation and quoting.[/li]
[li]Then perform substitutions wherever permitted.[/li]
[li]Finally, execute the result, using the first word as the command to execute, and passing all other words as arguments to the command.[/li][/ol]
This came into play in your example, because the variable substitution occurred before execution of your label command. Therefore, when the Tcl interpreter evaluated the label command, after the substitution pass, it had the equivalent of:

[tt]label .inputframe.label2 -text <NONE SELECTED YET>[/tt]

The string was in essence &quot;hard-coded&quot; into the command.

That's why several Tcl widgets, including the label, support a -textvariable option, which allows you to provide the name of a variable as an argument. Tcl then automatically updates the widget whenever the value of the variable changes. Thus, the correct command you want is:

[tt]label .inputframe.label2 -textvariable scriptsel[/tt]

(Notice: No &quot;$&quot; in front of the variable name. We need to provide the name of the variable, not its value.) - Ken Jones, President
Avia Training and Consulting
866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top