namespaces - how do I update a variable via a tk window by name -
consider following situation:
namespace eval ::mydialog {} proc ::mydialog::show {w varname args} { upvar 1 $varname thevar # can access thevar # (1) # code defining/creating window # here widgets user interaction created, # of call ::mydialog::_somecallback wm protocol $w wm_delete_window [list ::mydialog::close $w] } proc ::mydialog::_somecallback {} { # how access thevar here? # (2) } proc ::mydialog::close { w } { # here changes supposed written varname in calling scope, # how do that?! # (3) destroy $w }
im trying figure out how (a) variable calling scope (b) have available in 3 procs , (c) writing changes said variable.
(a) solve using 'upvar 1 $varname thevar' (b) solve namespace variable (c) long have 1 proc happen automaticly (a) due fact working on local alias of variable
the problem upvar works (at least intended) in (1). use upvar in (1) , save/copy namespace variable, solve (a) , (b), not (c).
i gratefull if point me in right direction here.
also, i'm relativly new tcl/tk concept might not ideal, suggestions toward better design welcome too.
i suggest use namespace variable keeps name of variable, , upvar
using global scope.
namespace eval ::mydialog { variable varname } proc ::mydialog::show {w _varname args} { variable varname $_varname upvar #0 $varname thevar } proc ::mydialog::_somecallback {} { variable varname upvar #0 $varname thevar puts $thevar } proc ::mydialog::close { w } { variable varname upvar #0 $varname thevar set thevar newval } set globalvar oldval # => oldval ::mydialog::show {} globalvar ::mydialog::_somecallback # => oldval ::mydialog::close {} # => newval puts $globalvar # => newval
note syntax highlighting fails: #0 $varname thevar
isn't comment.
this works namespace variables too: if have variable called nsvar
in ::foobar
namespace can use this:
set ::foobar::nsvar oldval ::mydialog::show {} ::foobar::nsvar ::mydialog::_somecallback ::mydialog::close {} puts $::foobar::nsvar
with same effects.
you can't, however, use variables local procedure way.
one way make simple use snit widgets instead of collections of tcl procedures.
documentation: namespace, proc, puts, set, upvar, variable
snit documentation: man page, faq (the faq serves kind of introduction well)
Comments
Post a Comment