# Whats is the real difference between this two examples of using Dynamic with Grid

the question is relatively simple I think. This is the first example of the code which works:

DynamicModule[{tabClicked, buttonClicked},
TabView[{
"First" ->
Dynamic@Grid[{{ Button["xx", tabClicked = 2; buttonClicked = 1],
Button["xx", tabClicked = 2; buttonClicked = 2]}}],
"Second" -> Dynamic[buttonClicked]
}, Dynamic[tabClicked]
]
]


while this doesn't:

DynamicModule[{tabClicked, buttonClicked},
buildButton[idx_] :=
Button["xx", tabClicked = 2; buttonClicked = idx];
TabView[{
"First" -> Dynamic@Grid[{{ buildButton[1], buildButton[2]}}],
"Second" -> Dynamic[buttonClicked]
}, Dynamic[tabClicked]
]
]


I know I can "fix" it by removing the Dynamic in front of Grid. The problem is that in the real example I need it as the grid context and even it's size are dynamic. I would like to understand why separating construction of button's into a function I get this strange behavior. Looking at the variable names using InputForm[] shows that the presence of Dynamic in front of Grid somehow forces Mathematica to assign temporary names to variables used in the buildButton. Many Thanks, Lukasz

-
The second tab does not work on the first code either. – Öskå Jun 28 '13 at 15:35
Welcome to SE Mathematica. It would be useful if you would state exactly what you are hoping the program will do. Do you really want two buttons in the first tab and none in the second? Whats wrong with TabView[{1, 2}]? – DavidC Jun 28 '13 at 15:37
Thank you, it is a very stylized example of a larger problem. Here effectively I would like to have behavior like in example two, where once you click a button the TabView shows the second tab and displays the value associated with the button which was clicked. In real case I would simply display some detailed information about the object which is displayed in a given row of the table on the first tab. – user8304 Jun 28 '13 at 15:58

Updated solution

Here is another way that works. It is perhaps to be preferred to the other two from a programming style point of view. In the OP, the function buildButton depends on external variables. It would be better to pass these variables to the function. In this case, since the variables are to be altered, the symbols need to be passed to the function unevaluated. Given the context I would choose to wrap the variables in Dynamic, in the manner of controls. The definition of buildButton may appear inside or outside the DynamicModule. It makes no difference, in fact.

buildButton[idx_, Dynamic[tab_], Dynamic[button_]] := Button["xx", tab = 2; button = idx];
DynamicModule[{tabClicked, buttonClicked},
TabView[{
"First" ->
Dynamic@Grid[{{buildButton[1, Dynamic[tabClicked], Dynamic[buttonClicked]],
buildButton[2, Dynamic[tabClicked], Dynamic[buttonClicked]]}}],
"Second" -> Dynamic[buttonClicked]}, Dynamic[tabClicked]]
]


Original solutions

Two solutions:

Put buildButton in the DynamicModule:

DynamicModule[{tabClicked, buttonClicked, buildButton},
buildButton[idx_] := Button["xx", tabClicked = 2; buttonClicked = idx];
TabView[{"First" -> Dynamic@Grid[{{buildButton[1], buildButton[2]}}],
"Second" -> Dynamic[buttonClicked]}, Dynamic[tabClicked]]]


Or change DynamicModule to Module

Module[{tabClicked, buttonClicked},
buildButton[idx_] := Button["xx", tabClicked = 2; buttonClicked = idx];
TabView[{"First" -> Dynamic@Grid[{{buildButton[1], buildButton[2]}}],
"Second" -> Dynamic[buttonClicked]}, Dynamic[tabClicked]]]


It has to do with when the symbols get processed by the front end from something like \$CellContextbuttonClicked$$ to FEbuttonClicked$$26, I think. If they don't get the same number after the $$, they are really different variables. Edit update: It seems in the OP's code the variables tabClicked and buttonClicked get assigned symbols like tabClicked44774 and buttonClicked44774, while the variables in the TabView get assigned symbols like FEtabClicked$$26 and FEbuttonClicked$$26. However, I have been unable to figure out why this happens. A halfway guess is that the definition of buildButton happens in the kernel, and tabClicked44774 etc. look like Module symbols instead of DynamicModule symbols (e.g. FEtabClicked$$26). This is what led me to suggest putting buildButton in the list of DynamicModule` variables, by the way.

-
Thank you!! It works. – user8304 Jun 28 '13 at 17:09