In the tcl / tk child window, I cannot set the default value for my input widget

I am a complete newbie in the tcl / tk world, but I tried to explore this on my own and continue to be empty.

I am expanding the tcl / tk application that allows me to add code to create a child window and do what I need. The problem is that when I create this window and try to set the default value for my input widget, it always appears empty.

Since then I have created an ultra simple demo application to reproduce this:

#!/usr/local/bin/wish set myvar1 "initial value 1" entry .entry1 -textvariable myvar1 button .spawnchild -text "Spawn Child" -command "spawn_click" pack .entry1 .spawnchild proc spawn_click {} { set myvar2 "initial value 2" toplevel .lvl2 entry .lvl2.entry2 -textvariable myvar2 entry .lvl2.entry3 -textvariable myvar1 pack .lvl2.entry2 .lvl2.entry3 } 

As you can see, the first window contains an input widget that has a default value of "initial value 1" and it displays correctly. When I click the "Spawn Child" button, a child window is created. As you can see, it has two built-in widgets. Each of them has a default value, and the first - using the default value that was created in its own area, and the entries below, using the default value in the main program area.

The problem is that the top input box for some reason does not show the default value, and the bottom one is just fine.

Multiple Popup Windows

Can someone explain this behavior and how to get the widget with the top row to correctly display its default value?

EDIT

Thanks Andrew and schlenk, it seems like this was the case of RTFM :) I checked your global suggestions and worked as promised. Thanks for setting me straight!

+4
source share
2 answers

myvar2 needs to be defined globally. Define spawn_click as follows:

 proc spawn_click {} { global myvar2; # myvar2 is a global variable set myvar2 "initial value 2" toplevel .lvl2 entry .lvl2.entry2 -textvariable myvar2 entry .lvl2.entry3 -textvariable myvar1 pack .lvl2.entry2 .lvl2.entry3 } 

and you have to be good ...

enter image description here

+4
source

Read the -textvariable documentation. The variable name must be global (or a fully qualified namespace), but your variable myvar2 is a local variable. This way you set a different variable in your proc than the textvariable variable uses.

An easy solution for your problem would be to write proc as follows:

 proc spawn_click {} { global myvar2 set myvar2 "initial value 2" toplevel .lvl2 entry .lvl2.entry2 -textvariable myvar2 entry .lvl2.entry3 -textvariable myvar1 pack .lvl2.entry2 .lvl2.entry3 } 

By explicitly declaring myvar2 as global scope, you must use the same variable in your -textvariable binding.

+4
source

All Articles