Message Boards Message Boards

0
|
4491 Views
|
1 Reply
|
1 Total Likes
View groups...
Share
Share this post:

Reload result of a simple operation using PopupMenu[]

Posted 9 years ago

Hi, I'm quite new using Mathematica and my English is horrible ... but I hope that maybe someone can help me.

I've written the following code, and seems to work. But by changing the selection field, recognizes the change until a new run. How I could change my code for my result variable is updated to automatically enter a new team?

Any track will be appreciated, thanks

CreateWindow[DialogNotebook[{

    TextCell["Enter the type of operation to be performed:"],
    DynamicSetting[
     PopupMenu[Dynamic[list],
      {
       "Sum", "Substraction", "Multiplication", "Division", "Powers", 
       "Roots"
       }
      ]
     ],

    TextCell["Enter the first operand: "],
    DynamicSetting[InputField[Dynamic[num1], Expression]],
    TextCell["Enter the second operand: "],
    DynamicSetting[InputField[Dynamic[num2], Expression]],

    (*Realiza la operación de acuerdop al campo de selección*)

    If[list == "Sum", result = Dynamic[num1 + num2], Null];
    If[list == "Substraction", result = Dynamic[num1 - num2], Null];
    If[list == "Multiplication", result = Dynamic[num1*num2], Null];
    If[list == "Division", result = Dynamic[num1/num2], Null];
    If[list == "Powers", result = Dynamic[num1^num2], Null];
    If[list == "Roots", result = Dynamic[num1^(1/num2)], Null];

    TextCell["Its outcome is: "],
    Dynamic[result],

    ChoiceButtons[{DialogReturn[Dynamic[result]]}]
    },

   WindowTitle -> "Basic Operation"
   ],
  WindowSize -> All
  ];

Your problem occurs because the series of If statements are only evaluated once, when the dialog window is initially made. Here, I replace them with a single Switch statement inside a Dynamic, so it's evaluated every time something changes.

CreateWindow[
  DialogNotebook[{TextCell[
     "Enter the type of operation to be performed:"], 
    DynamicSetting[
     PopupMenu[
      Dynamic[list], {"Sum", "Substraction", "Multiplication", 
       "Division", "Powers", "Roots"}]], 
    TextCell["Enter the first operand: "], 
    DynamicSetting[InputField[Dynamic[num1], Expression]], 
    TextCell["Enter the second operand: "], 
    DynamicSetting[InputField[Dynamic[num2], Expression]],
    (*Realiza la operación de acuerdop al campo de selección*)
    TextCell["Its outcome is: "], Dynamic[Switch[list,
      "Sum", num1 + num2,
      "Substraction", num1 - num2,
      "Multiplication", num1*num2,
      "Division", num1/num2,
      "Powers", num1^num2,
      "Roots", num1^(1/num2)]], 
    ChoiceButtons[{DialogReturn[Dynamic[result]]}]}, 
   WindowTitle -> "Basic Operation"], WindowSize -> All];
POSTED BY: Jesse Friedman
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract