简体   繁体   English

无法创建动态变量

[英]Failed to create dynamic variable

I'm trying to create the same numbered variable, but there's something that stops it.我正在尝试创建相同的编号变量,但有些东西阻止了它。 But I haven't figured out what it could be yet.但我还没有弄清楚它可能是什么。

ie IE

set txt0 ""
set txt1 ""
set txt3 ""

So I'm trying to do this dynamically with every click on the button.所以我试图在每次点击按钮时动态地执行此操作。 See my code:看我的代码:

frame .top.tab

button .top.tab.btnTab -text "+" -command { bell ; add }

frame .top.tool
.top.tool configure -relief "raised"

frame .top.panel
.top.panel configure -bg "white"

set n 0

proc add {} {

    global n

    set txt$n ""

        entry .top.tool.ent$n -textvar txt$n

        button .top.tool.btn$n -text txt$n -command " remove $n ; .top.panel.lbl$n config -textvar $txt$n "

        pack .top.tool.ent$n .top.tool.btn$n -side left
incr n
        label .top.panel.lbl$n -text "" -bg "white"

        pack .top.panel.lbl$n -fill both -expand yes -anchor e

}

pack .top.tab -side top -fill x -anchor nw
pack .top.tab.btnTab -side right

proc remove { number } {    

    set total 2

    for { set i 0 } { $i < $total } { incr i } {                    
                    
        pack forget .top.panel.lbl$i
        
    }
    
    pack forget .top.panel.lbl$total
        
    pack .top.panel.lbl$number -fill both -expand yes -anchor e
}

pack .top.tool -side top -fill x -anchor nw
pack .top.panel -side top -fill both -expand yes -anchor sw

What could it be?会是什么呢?

I know this around the variable $txt$n我知道变量$txt$n

You're creating a local variable with the name you want, but Tk won't bind anything to local variables as widgets typically outlast stack frames.您正在创建一个具有所需名称的局部变量,但 Tk 不会将任何内容绑定到局部变量,因为小部件通常比堆栈框架更持久。 You also want to be careful about when you are dealing with the name of a variable versus the current content of the variable.您还需要小心处理变量名称与变量当前内容的时间。

In the simple case, the best approach is to use an element of a global array:在简单的情况下,最好的方法是使用全局数组的元素:

proc add {} {
    global n txt

    set txt($n) ""

    entry .top.tool.ent$n -textvar txt$n
    button .top.tool.btn$n -text txt$n -command \
        " remove $n ; .top.panel.lbl$n config -textvar txt($n) "
    pack .top.tool.ent$n .top.tool.btn$n -side left
    
    incr n
    
    label .top.panel.lbl$n -text "" -bg "white"
    pack .top.panel.lbl$n -fill both -expand yes -anchor e
}

In more complex cases, consider using a TclOO object to hold the state;在更复杂的情况下,考虑使用 TclOO 对象来保存状态; handles to those are usually "simple" words (unless you take special steps to make them not be; most programmers simply aren't that devious normally).这些句柄通常是“简单”的词(除非您采取特殊步骤使它们不是;大多数程序员通常不会那么狡猾)。

With the observation and suggestions given in the response of Donal Fellows , I did the necessary changes and worked as expected.根据Donal Fellows的回复中给出的观察和建议,我进行了必要的更改并按预期工作。 In the lines where the property -textvar has just been able to make the exchange of its value by the txt($n) variable that Donal Fellows pointed at the answer.在属性-textvar刚刚能够通过txt($n)变量交换其值的行中, Donal Fellows指出了答案。 But it has not worked out in my code, since I had to make another change incr n , change its position of the middle of the logic to the end of the proc add scope was vital.但它在我的代码中没有解决,因为我不得不进行另一个更改incr n ,将其逻辑中间的位置更改为proc add范围的末尾是至关重要的。 Ready, the logic worked as expected.准备就绪,逻辑按预期工作。 Thanks in advance.提前致谢。

before

在此处输入图像描述

after

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM