2011-10-21 4 views
1

呼び出し元の名前空間から変数にアクセスし、変数を読み込み、変更できるプロシージャが必要です。変数は_current_selectionとなります。私はいくつかの異なる方法でupvarを使ってそれをしようとしましたが、何も働いていませんでした。 (ちょうどupvar機構をテストするために小さなテスト手順を書いた)。 PROCへTclでupvarを使用しているプロシージャに変数を渡すことができません


コール:

select_shape $this _current_selection 

PROC:

コールPROCへ:私の第二の試みのために

proc select_shape {main_gui var_name} { 
    upvar $var_name curr_sel 
    puts " previously changed: $curr_sel" 
    set curr_sel [$curr_sel + 1] 
} 

ここに私の試みです
select_shape $this 

PROC:すべての試みで

proc select_shape {main_gui} { 
    upvar _current_selection curr_sel 
    puts " previously changed: $curr_sel" 
    set curr_sel [$curr_sel + 1] 
} 

、それはコードで、この領域に達すると、それは私が間違って何をやっているcan't read "curr_sel": no such variable

を言いますか?

EDIT:関数の呼び出しがbindコマンドから作られている

:私はそれは問題ではないと思っ開始時に

$this/zinc bind current <Button-1> [list select_shape $this _current_selection] 

。しかし、おそらくそれはします。変数を動作させるupvarため

+1

私は '$ this'変数を参照してください。 incrTclクラスを使用していますか?名前空間と同じではありません。 – GrAnd

+0

@GrAnd、私は本当に知りません。 – SIMEL

+0

@GrAnd:同意しました。 'upvar'自体の使用自体はOKです。何か他のことが起こっている。 _really_重要な詳細がありません。 –

答えて

4

は、私は、変数が発見されることが期待されるところですのでbindのコマンドは、グローバル名前空間で動作することを信じています。これはうまくいくかもしれません:

$this/zinc bind current <Button-1> \ 
    [list select_shape $this [namespace current]::_current_selection] 
2

は、あなたがそれを呼び出している範囲内に存在する必要があります以下の点を考慮してください。

proc t {varName} { 
    upvar $varName var 
    puts $var 
} 

#set x 1 
t x 

そのままあなたがそれを実行した場合、あなたが報告されているエラーが発生します、 セットx 1行のコメントを外してください。

0

以下の例では、他の名前空間から変数を変更するほとんどのバリエーションをカバーしようとしました。それは私のために100%働く。多分それが助けになるでしょう。

proc select_shape {main_gui var_name} { 
    upvar $var_name curr_sel 
    puts " previously changed: $curr_sel" 
    incr curr_sel 
} 

namespace eval N { 
    variable _current_selection 1 
    variable this "some_value" 

    proc testN1 {} { 
    variable _current_selection 
    variable this 
    select_shape $this _current_selection 
    puts " new: $_current_selection" 
    } 

    # using absolute namespace name 
    proc testN2 {} { 
    select_shape [set [namespace current]::this] [namespace current]::_current_selection 
    puts " new: [set [namespace current]::_current_selection]" 
    } 

    select_shape $this _current_selection 
    puts " new: $_current_selection" 
} 

N::testN1 
N::testN2 

#------------------------------------- 
# Example with Itcl class 
package require Itcl 

itcl::class C { 
    private variable _current_selection 10 

    public method testC {} { 
    select_shape $this [itcl::scope _current_selection] 
    puts " new: $_current_selection" 
    } 
} 

set c [C#auto] 
$c testC 
関連する問題