Sunteți pe pagina 1din 2

Entry Variables and Procs Page 1

Dear all,

Katherine's issue is one I've run across several times, so I used this as
an excuse to figure it out for myself once and for all. I thought others
might be interested, so I summarized a couple of things about it:

The Problem: When you create an entry in Tcl/Tk, you associate a Tcl
variable with it. The problem arises when you want to use that variable to do something
from within a proc (proc's are subroutines in Tcl) and the entry was made in a different
proc.

The problem is that variables are local by default


so you need to either pass them or somehow share them with proc's There are two ways
to do this:

Real lazy way:

Build all your entry widgets in your main routine so they are global by default
(makes for messy code, but OK for simple things)

Simple way:

Just declare the variable as global in the procs that it is used in.
NOTE: You can not use global if you are using namespaces

The Right Way:

The proper way to do this is to place your entire routine into a namespace
Once you have done that, any time you reference a variable that is shared
across proc's you prefix it with namespace::
This is the "right way" because you don't have to worry about overwriting
a global variable that someone else defined.
However, I hate typing namespace:: on everything so I have never
gotten used to using this method.

Here is an example with name spaces:


#---------------------------------------------------------------------------
-----------------------
namespace eval exmp1 {
set t .exmp1
catch {destroy $t}
toplevel $t

label $t.msg -text "Enter Something" -padx 2m -pady 2m


entry $t.entry -relief sunken -textvariable exmp1::myVar
button $t.okBut -text "OK" \
-command exmp1::doOK
# -command [ namespace code [list doOK] ]

pack $t.msg
pack $t.entry
pack $t.okBut

proc doOK {} {
puts $exmp1::myVar
destroy $exmp1::t
}
}
#---------------------------------------------------------------------------
-----------------------

If you want to use globals:

#---------------------------------------------------------------------------
-----------------------
proc bldIt {} {
global t myVar
label $t.msg -text "Enter Something" -padx 2m -pady 2m
entry $t.entry -relief sunken -textvariable myVar
button $t.okBut -text "OK" -command doOK

pack $t.msg

http://www.padtinc.com/sup... 30/11/2008 5:24:33


Entry Variables and Procs Page 2

pack $t.entry
pack $t.okBut
}

proc doOK {} {
global t myVar
puts $myVar
destroy $t
}

set t .exmp2
catch {destroy $t}
toplevel $t
bldIt
#---------------------------------------------------------------------------
-----------------------

If you look closely at the last example, you will note that the widgets
are built in a proc. If you build your widgets in the main routine,
AND you don't use namespaces, you don't need the globals:

#---------------------------------------------------------------------------
-----------------------
set t .exmp2 catch {destroy $t}
toplevel $t

label $t.msg -text "Enter Something" -padx 2m -pady 2m


entry $t.entry -relief sunken -textvariable myVar
button $t.okBut -text "OK" -command doOK

pack $t.msg
pack $t.entry
pack $t.okBut

proc doOK {} {
puts $myVar
destroy $t
}
#---------------------------------------------------------------------------
-----------------------

I hope this is helpful

Eric

-------------------------------------------------------------------------
Eric Miller
Director, CAD &
Software Services
Phoenix Analysis &
Design Technologies
(480) 813-4884, x103
www.padtinc.com

-----------------------------------------------------------------

TO SUBSCRIBE: send blank email to xansys-subscribe@yahoogroups.com


TO UNSUBSCRIBE: send blank email to xansys-unsubscribe@yahoogroups.com

ARCHIVED on: http://www.groups.yahoo.com/messages/xansys

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/

http://www.padtinc.com/sup... 30/11/2008 5:24:33

S-ar putea să vă placă și