1
$\begingroup$

This question grew out of this other one: DynamicModule not working when additional code is included.

Let's start with this simple code:

colour = "red";
Panel[
 Column[{
   InputField[colour, String],
   Do[
    n1 = 4,
    {i, 1, 3}
    ],
   InputField[colour, String]
   }]
 ]

The output is the following:

panel1.jpg

The Do loop returns a Null, which shows up as a blank space between the two text boxes. Now use DeleteCases to get rid of the Null:

colour = "red";
Panel[
 Column[
  DeleteCases[
   {
    InputField[colour, String],
    Do[
     n1 = 4,
     {i, 1, 3}
     ],
    InputField[colour, String]
    }, Null]
  ]
 ]

And the output is now without the Null:

panel2.jpg

Now add a Dynamic (which I need for the other question I referred to), as follows:

colour = "red";
Panel[
 Column[
  DeleteCases[
   {
    InputField[colour, String],
    Dynamic[Do[
      n1 = 4,
      {i, 1, 3}
      ]],
    InputField[colour, String]
    }, Null]
  ]
 ]

And here is the output:

panel3.jpg

In other words, Dynamic seems to be shielding from the action of DeleteCases. Is there any way to get rid of the Null in the output in this third case?

Many thanks.

$\endgroup$
  • $\begingroup$ Why are you performing Do loop in a Column? p.s. the reason why is: mathematica.stackexchange.com/questions/5817/… $\endgroup$ – Kuba Nov 15 '16 at 19:25
  • $\begingroup$ Good question! The short answer is that I need a prototype of an interactive query tool: user enters a name in the first textbox, the name is matched in a Do loop here but eventually with a mysql query, and the array index corresponding to the name is used to display a time series in the third element of this column. For this question I used another textbox but the question this refers to shows a plot. I have no idea if this is a dumb way to code it. I have not used MMA's GUI before so I am in hacking mode at the moment. ACL's answer is excellent, thanks for the pointer! $\endgroup$ – pdini Nov 15 '16 at 22:48
2
$\begingroup$

You need to display the content of Dynamic, otherwise it won't evaluate. However, you can hide it if you combine your inputs like this:

colour = "red";
Panel[Column[
  {Multicolumn[
    {InputField[colour, String], 
     Style[Dynamic[Do[n1 = 4, {i, 1, 3}]], Transparent]}, {1, 2}],
    InputField[colour, String]}
]]
$\endgroup$
  • $\begingroup$ Excellent! This works. Incredible, I would have never thought of using Style/Transparent, very ingenious. The Multicolumn part is not needed since I don't mind the extra space between the two graphical objects, but it's good to know in any case. Thank you very much! $\endgroup$ – pdini Nov 15 '16 at 22:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.