I'm fairly new to Mathematica and currently making a simple GUI.

I'd like to work with dropdown menus and I'd like to dynamically change the possibilites of those menus depending on what's selected in the previous menu.

Using a ton of If-functions seemed impractical to me and so I ended up using the Piecewise-function instead (which might be stupid as well).

   (*Dynamische Eingabefeld-Abhängigkeiten*)
Dynamic[Piecewise[{{forminput={"Viereck","Kreis"},method=="2D"},{forminput={"Quader", "Zyllinder"},method=="3D"}}]]
    {diminput=Column[{"Länge: "InputField[Dynamic[l],Number], "Breite: "InputField[Dynamic[b],Number]}], form=="Viereck"},
    {diminput=Column[{"Radius: "InputField[Dynamic[r],Number]}], form=="Kreis"},
    {diminput=Column[{"Länge: "InputField[Dynamic[l],Number], "Breite: "InputField[Dynamic[b],Number], "Höhe: "InputField[Dynamic[h],Number]}], form=="Quader"},
    {diminput=Column[{"Radius: "InputField[Dynamic[r],Number], "Höhe: "InputField[Dynamic[h],Number]}], form=="Zyllinder"}

(*GUI Input-Oberfläche*)
    {Style["2D oder 3D mesh?", Bold]},
    {Style["Geometrische Form:", Bold], Style["Dimensionen [mm] :", Bold]},
    {Dynamic[PopupMenu[Dynamic[form],forminput]], Dynamic[diminput]} 

The code works as intended, however I cannot put a ";" behind the Piecewise-Functions. Once I do that it stops working. At the same time, having the evaluation of those functions permanently shown kinda ruins the look.

My questions would be:

1) is there a smarter way to hoave dependent drop down menus?

2) is there a way to surpress the output of the Piecewise-Functions without having them to stop working?


1 Answer 1


0) Piecewise is more for math applications. I'd use Switch as more readable here: Switch[method, "2D", {"Viereck", "Kreis"}, "3D", {"Quader", "Zyllinder"}] etc.

2) Correct, with your approach you need to keep first two Dynamic visible. See Dynamic and tutorials linked at the bottom of its documentation. Other ways to react to changes of certain variable is to use DynamicWrapper or the second argument of Dynamic

DynamicWrapper[ controller[Dynamic[var]], var2 = something[var], TrackedSymbols :> {var}]


controller[Dynamic[var, {Automatic, (var2 = something[var])&}]]

See docs for more details.

1) Here is my implementation, and I don't really use any of methods mentioned above. I just make form menu dynamic based on method value instead of having an additional forminput variable.

DynamicModule[{method = "2D", forms, form = "Viereck", dims, l, b, r, 
    {Style["2D oder 3D mesh?", Bold]},
    {PopupMenu[Dynamic[method], {"2D", "3D"}]},
    {Style["Geometrische Form:", Bold], Style["Dimensionen [mm] :", Bold]},
    {Dynamic[PopupMenu[Dynamic[form], forms[method]]], 
     Dynamic[Grid[dims[form], Alignment -> {Left, Center}]]}
 Initialization :> (
   forms[method_] := 
    Switch[method, "2D", {"Viereck", "Kreis"}, 
     "3D", {"Quader", "Zyllinder"}];

   dims[form_] := Switch[form,
     "Viereck", {{"Länge:", 
       InputField[Dynamic[l], Number]}, {"Breite:", 
       InputField[Dynamic[b], Number]}},
     "Kreis", {{"Radius:", InputField[Dynamic[r], Number]}},
     "Quader", {{"Länge:", 
       InputField[Dynamic[l], Number]}, {"Breite:", 
       InputField[Dynamic[b], Number]}, {"Höhe:", 
       InputField[Dynamic[h], Number]}},
     "Zyllinder", {{"Radius:", 
       InputField[Dynamic[r], Number]}, {"Höhe:", 
       InputField[Dynamic[h], Number]}}
  • $\begingroup$ Thanks, I ended up using Switch and assigned the strings to values. for example: PopupMenu[Dynamic[method],{0->Spacer[0],1->"2D",2->"3D"}]. Really like how you made seperate functions for that, which makes "designing" the Grid look much more structured. Still have to wrap my head around what DynamicModule does though! $\endgroup$
    – p4di
    Commented Apr 24, 2018 at 13:34
  • $\begingroup$ @p4di here it is mostly for localization of those symbols and also for storing definitions of forms and dims so you can deploy it as cdf and it will work. $\endgroup$
    – Kuba
    Commented Apr 24, 2018 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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