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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Enable/Disable Widget

Status
Not open for further replies.

mistkhan

Technical User
May 31, 2003
14
0
0
IN
Hi All

I have written a code to enable/disable a text and entry widget on selection of a checkbutton, I was able to create it.However I have some queries

1. "m" variable is nothing but $w.bottom. I tried

proc lfEnableButtons {w} {
foreach child [winfo children $w] {
if {$child == "$m.text"} continue
if {$::test_suite_lindow} {
$child configure -state normal
} else {
$child configure -state disabled
}
}
}

This code didnt work , can anyone tell me what is the problem in this ?.

2. Earlier my code was like this ie call the checkbutton option first and then set the variable m , create text , bcos of which I got the error "m" unknown. I solved the problem by creating the text first and then the checkbutton option. I wanted to know , whether there is any other solution to overcome this?. I tried setting set m 0 , in the beginning of the code , later even after setting new value for "m", it was 0 always

checkbutton $w.cb2 -text {Download} -variable test_suite_lindow -command [list lfEnableButtons $w $m]
set m [frame $w.bottom]
text $m.text -yscrollcommand "$m.yscr set" -height 10 -width 60 -wrap none
scrollbar $m.yscr -orient vertical -command "$m.text yview"
pack $m.text -side left
pack $m.yscr -side right -fill y
pack $m

Thanking you in advance
Regards
Aman


#########CODE STARTS FROM HERE#####################################################

proc lfEnableButtons {w m} {


if {$::test_suite_lindow} {
$w.e1 configure -state normal
$w.e2 configure -state normal
$w.e3 configure -state normal
} else {
$w.e1 configure -state disabled
$w.e2 configure -state disabled
$w.e3 configure -state disabled
}
if {$::test_suite_lindow} {
$m.text configure -state normal

} else {
$m.text configure -state disabled
}
}


# entry1.tcl --
#
# This demonstration script creates several entry widgets without
# scrollbars.
#
# RCS: @(#) $Id: entry1.tcl,v 1.2 1998/09/14 18:23:28 stanton Exp $

set w .entry1
set entryvar 0
set test_suite_lindow 0
set test_suite_lindow1 0
catch {destroy $w}
toplevel $w

wm title $w "Entry Demonstration (no scrollbars)"
wm iconname $w "entry1"


label $w.msg -wraplength 5i -justify left -text "Three different entries are displayed below. You can add characters by pointing, clicking and typing. The normal Motif editing characters are supported, along with many Emacs bindings. For example, Backspace and Control-h delete the character to the left of the insertion cursor and Delete and Control-d delete the chararacter to the right of the insertion cursor. For entries that are too large to fit in the window all at once, you can scan through the entries by dragging with mouse button2 pressed."
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"
button $w.buttons.code -text "See Code" -command "showCode $w"
button $w.buttons.ok -text "OK" -command "showVars $w.dailog entryvar"
pack $w.buttons.dismiss $w.buttons.code $w.buttons.ok -side left -expand 1

set m [frame $w.bottom]
text $m.text -yscrollcommand "$m.yscr set" -height 10 -width 60 -wrap none
scrollbar $m.yscr -orient vertical -command "$m.text yview"
pack $m.text -side left
pack $m.yscr -side right -fill y
pack $m


entry $w.e1 -textvariable entryvar
entry $w.e2
entry $w.e3
checkbutton $w.cb2 -text {Download} -variable test_suite_lindow -command [list lfEnableButtons $w $m]

$m.text insert 1.0 "hi"
$m.text insert end "hi"
pack $w.e1 $w.e2 $w.e3 $w.cb2 -side top -pady 5 -padx 10 -fill x

$w.e1 insert 0 "Initial value"
$w.e2 insert end "This entry contains a long value, much too long "
$w.e2 insert end "to fit in the window at one time, so long in fact "
$w.e2 insert end "that you'll have to scan or scroll to see the end."
lfEnableButtons $w $m
#########CODE ENDS FROM HERE#####################################################

 
In Tcl, variables are inside 'environments' or namespaces.

All the variables declared inside a procedure are know inside this procedure.
All other variables are not.
Well.. All variables defined outside the procedure and not declared inside the procedure are not known.

All the variables declared outside any procedure are global variables. Without doing something they are not known inside procedures.
More later.

Your m variable is declared outside any procedure so is a global variable and is not known inside the lfEnableButtons procedure.
To have the m variable known inside the procedure you can:
- declare in the procedure the m variable as global:
Add 'global m' in the first statements of the proc.
- use ::m in place of m:
The :: indicates that the variable is a global variable (precisely a variable of the global namespace).

More on environments.

An environment deals with variables.
A procedure knows only its local environment.
But it can be augmented:
- with the global command, as seen.
- with the variable command: this command adds a variable of the embedding namespace of the proc (a namespace can contain variable and proc names).
- with the upvar command: this command adds a variable from the outer proc environments.
And the uplevel command changes the environment of the proc to an outer environment.

Environments are for variables: by default the procs are in the global namespace or can be in a named namespace.

More on namespaces.

Namespaces contains variable and proc names.
They have names (with the exception of the global namespace which has a blank name).
A var or a proc of the ::ns namespace can be referred by ::ns::var or ::ns::proc anywhere.
A var or a proc of the :: namespace (the global namespace) can be referred by ::var or ::proc anywhere.

A proc inside a namespace can reference any proc of the namespace. If a called proc is not found in the namespace the global namespace is searched. No other namespace is searched.

A proc of a namespace cannot reference a variable of the namespace (without some effort). With the variable command a proc can add a variable of its namespace to its environment and can then reference it.

All this is a little bit confusing.
So my advice: for global variables use ::name.
A second advice: spend some time to read the Tcl documentation.

HTH

ulis
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top