I have written code, with the help of stackoverflow of course, and I want to make it user friendly so that other people in my lab can use it. I'm playing with DialogCreate and similar functions. I want to do something like this so far:
CreateDialog[TabView[{
"General" ->
Column[{
Row[{TextCell["Project Name:"], InputField[Dynamic[Project]]}],
Row[{TextCell["Number of Wells:"], InputField[Dynamic[num]],
Button["Set", DialogReturn[num]]}]}],
"Row Selection" ->
Column[{Do[
Row[TextCell["Well " <> ToString[i] <> ":"],
FileNameSetter[Dynamic[Evaluate[Symbol["w" <> ToString[i]]]],
"Open"]], {i, 1, num}]}]}, ControlPlacement -> Left]
]
I want to dynamically create more rows based on the input of "Number of Wells" within the same dialogbox but in a different tab. Is anything like this possible or some sort of alternative?
Answer
CreateDialog[
TabView[
{"General" ->
Column[{Row[{"Project Name:", InputField[Dynamic[project]]}],
Row[{"Number Of Wells:", InputField[Dynamic[num]],
Button["Set", DialogReturn[num]]}]}],
"Row Selection" ->
Dynamic@Column[
Table[Row[{"Well " <> ToString[i] <> ":",
FileNameSetter[Dynamic[Evaluate[Symbol["w" <> ToString[i]]]],
"Open"]}], {i, 1, num}]]},
ControlPlacement -> Left]]
Update: There are two problems in the original code:
First, regarding the use of Do
, unless an explicit Return
is used, the value returned by Do
is Null
. Please see the docs on Do
and the tutorial Loops and control structures. For example
t = 2; Do[t = 1 + k t, {k, 1, 2}]
does the calculation to change the value of t
but does not return anything. You need to explicitly call
the new value of t
to see the effect of what happened inside Do
:
t = 2; Do[t = 1 + k t, {k, 1, 2}];t
(* which gives the result: 7 *)
So, to get your code working with Do
, you need to do something like:
"Row Selection" ->
Dynamic@Column[tt = {};
Do[AppendTo[tt, Row[{"Well " <> ToString[i] <> ":",
FileNameSetter[Dynamic[Evaluate[Symbol["w" <> ToString[i]]]],
"Open"]}]],
{i, 1, num}]; tt]
which, of course, is not as clean as the alternative with Table[...]
.
Second, regarding why Dynamic[Column[ ...]]
is needed please see the tutorial Introduction to Dynamic, in particular, the section Where Should Dynamic Be Placed in an Expression?
By the way, @
is short form of Prefix
, and f@x
is an alternative syntax for f[x]
, that is, Dynamic@Column[.....]
is the same as Dynamic[Column[...]]
.
Comments
Post a Comment