Updated 2017-11-20 23:59:52 by mue

Got a strange behaviour when passing variables from one procedure to another. I don't know, if this is a bug or a feature. Also I didn't made an extensive search in this forum, because I don't know how and what to search for ... Pardon me, I'm not a professional programmer. So maybe this is a bit boring to you cracks!
 package require Tk

 proc Toplevel {var} {
        toplevel .top
        set liste {3 2}

        pack [ttk::label .top.lb2 -text "$var"]

        # comment me off -- the spinbox!
        pack [spinbox .top.sb -values $liste -textvariable var]

        pack [ttk::combobox .top.cb1 -values $liste -textvariable var]

        pack [ttk::combobox .top.cb2 -values $liste -textvariable b]
        puts $var

 proc mainwindow {} {
        set a 1
        set ::b 2
        pack [ttk::button .b -text "Push me" -command {Toplevel $::b}]


When I push the button of the main window, the lable in the toplevel windows tells the right value. Further more, the spinbox now tells the first value of the list (which is 3). Ok, strange, because textvariable var was sent from main window and should initiate the spinbox (in my opinion). Now var is set and the combobox tells the same. If you comment the spinbox off, the first combobox is left empty! Is it a bug? In addition, when passing over $a I got an error? Why? Does a has to be global? How to pass over a variable the right way?

Thanks a lot! PS. Sorry for my poor English, as I'm not a native speaker ...


Beware textvariable is always a global variable. I think what you need is a proc to initialise your top level and a seperate proc to change the values.

If you set a textvariable, updating the spin/combo boxes is then as easy as setting the variable.

MG A better way to do what you're doing would be to pass the variable name (::b) instead of its current value ($::b) to Toplevel from your button. Then all of your -textvariables would be "-textvariable $var" (which would eval to "-textvariable ::b"), and your label's -text would use
  -text [set $var]

to display the current value

[mue] Tnx! I see what you mean. As Beware said, textvars are always global, the answer was in front of me! My intention was to keep the variables local, inside the proc. But, hey, textvar is global so why do "Chinese Whispers" when in the end the variable is seen by everyone. Thank you very much for directing me to the solution ...