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 "/export/home/fabien/Cshscripts/"
set pathlength [string length $scriptspath]
global pathlength
# script selected
set scriptsel "<NONE SELECTED YET>"
global scriptsel
# global list
global slist
# main window
#
wm title . "AWK"
# browse(dir) is the directory containing all the AWK files
set browse(dir) $scriptspath
# set file containing the scripts
set menufile [append scriptspath "MENU"]
global browse
#general Menu
menu .menuBar -tearoff 0
set m .menuBar.file
.menuBar add cascade -menu $m -label "File" -underline 0
menu $m -tearoff 0
$m add command -label "Select Script" -command {Selectscript} -underline 0
$m add separator
$m add command -label "Exit" -underline 1 -command {exit} -accelerator "Meta-Q"
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 "Run" -command Run]
button .buttonframe.quit -background #ffbbbb -activebackground #ffbbbb -text "Quit/Cancel" -command exit
button .buttonframe.help -text "Help" -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 "Script selected: " -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 "xterm -e nawk -f decposlog"
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 "Error: Input data is required"
pack .msg -fill x -expand true
return
}
# running nawk script/Log
append script " $inputfile "
$log insert end "Command line:\n"
$log insert end $script\n
catch { puts $script }
if [catch { open "|$script |& cat" } 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 "Run it" -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 "Reading File $menufile, please wait...\n"
# test if can open themenu file
if { [file exists $menufile] } {
if { ![file readable $menufile] } {
# cannot find or open the file
tk_messageBox -title "Error: $menufile" -message "File cannot be found or read\n, please run xxx to create it" -type ok
exit
} else {
# the file exists read the data from it
AddText "Creating the menus..\n"
set c 0
set fd [open $menufile "r"]
set data [read $fd]
# process data file
set data [split $data "\n"]
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 "Done!\n"
}
}
}
# 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 "Select Script"
wm iconname $w "Scripts"
#label $w.msg -font $font -wraplength 4i -justify left -text "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"
#pack $w.msg -side top
frame $w.buttons
pack $w.buttons -side bottom -fill x -pady 2m
button $w.buttons.dismiss -text Dismiss -command "destroy $w"
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 "$w.frame.list yview"
listbox $w.frame.list -yscroll "$w.frame.scroll set" -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 "scriptsel: $scriptsel"
}
# 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 " help"
#
button .help.quit -text "Quit/Dismiss" -command "destroy .help"
#
# 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 "blblsaldsdlsdsd."
#
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
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 "/export/home/fabien/Cshscripts/"
set pathlength [string length $scriptspath]
global pathlength
# script selected
set scriptsel "<NONE SELECTED YET>"
global scriptsel
# global list
global slist
# main window
#
wm title . "AWK"
# browse(dir) is the directory containing all the AWK files
set browse(dir) $scriptspath
# set file containing the scripts
set menufile [append scriptspath "MENU"]
global browse
#general Menu
menu .menuBar -tearoff 0
set m .menuBar.file
.menuBar add cascade -menu $m -label "File" -underline 0
menu $m -tearoff 0
$m add command -label "Select Script" -command {Selectscript} -underline 0
$m add separator
$m add command -label "Exit" -underline 1 -command {exit} -accelerator "Meta-Q"
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 "Run" -command Run]
button .buttonframe.quit -background #ffbbbb -activebackground #ffbbbb -text "Quit/Cancel" -command exit
button .buttonframe.help -text "Help" -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 "Script selected: " -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 "xterm -e nawk -f decposlog"
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 "Error: Input data is required"
pack .msg -fill x -expand true
return
}
# running nawk script/Log
append script " $inputfile "
$log insert end "Command line:\n"
$log insert end $script\n
catch { puts $script }
if [catch { open "|$script |& cat" } 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 "Run it" -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 "Reading File $menufile, please wait...\n"
# test if can open themenu file
if { [file exists $menufile] } {
if { ![file readable $menufile] } {
# cannot find or open the file
tk_messageBox -title "Error: $menufile" -message "File cannot be found or read\n, please run xxx to create it" -type ok
exit
} else {
# the file exists read the data from it
AddText "Creating the menus..\n"
set c 0
set fd [open $menufile "r"]
set data [read $fd]
# process data file
set data [split $data "\n"]
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 "Done!\n"
}
}
}
# 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 "Select Script"
wm iconname $w "Scripts"
#label $w.msg -font $font -wraplength 4i -justify left -text "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"
#pack $w.msg -side top
frame $w.buttons
pack $w.buttons -side bottom -fill x -pady 2m
button $w.buttons.dismiss -text Dismiss -command "destroy $w"
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 "$w.frame.list yview"
listbox $w.frame.list -yscroll "$w.frame.scroll set" -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 "scriptsel: $scriptsel"
}
# 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 " help"
#
button .help.quit -text "Quit/Dismiss" -command "destroy .help"
#
# 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 "blblsaldsdlsdsd."
#
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