4
$\begingroup$

I would like to construct a GUI using just the Mathematica language that allows me to start and stop a calculation interactively with the press of buttons. I have set up the following to show what I wish to do, although you will notice that I can start the calculation, but I can't stop it.

Pressing Start will begin a count-up of num and dynamic update of the field to show num. I have a limit of 20 set so that the loop does not go on forever. I wish to be able to hit Stop along the way and halt the loop. Obviously, I need help figuring out a good way to do this through Dynamic, Monitor or some other construct.

DIAG2 := CreateDialog[
   
   Module[{buttonpar, startbutton, stopbutton, startcalc, stopcalc, 
     labels, fields, num, numlim, calc},
    
    numlim = 20;
    calc = False;
    
    labels = {"num"};
    fields = {InputField[Dynamic[num], Number, Enabled -> False]};
    
    startcalc[] := (
      num = 0;
      calc = True;
      Print["START"];
      While[calc && (num <= numlim), Pause[1]; num = num + 1; 
       num = num++]
      );
    
    stopcalc[] := (
      calc = False;
      Print["STOP"];
      );
    
    buttonpar = {ImageSize -> All, 
      BaseStyle -> {"Evaluate", 12, Bold}};
    startbutton = 
     Button["START", startcalc[], Evaluate@buttonpar, 
      Method -> "Queued", Enabled -> True];
    stopbutton = 
     Button["STOP", stopcalc[], Evaluate@buttonpar, 
      Method -> "Queued", Enabled -> True];
    
    Panel[TableForm[{startbutton, stopbutton, labels, fields}]]
    
    ],
   
   Modal -> False, WindowTitle -> "START/STOP TEST"
   
   ];

I run it with this:

DIAG2;

Thank you for your attention and help with this.

Ben

==================== UPDATE! ====================

Thanks to Gustavo's answer, I have modified the code to the following, which does what I had originally wanted. Now I have also include a Reset button as well as using a dynamic variable en for the Enabled parameter. This sets the Reset and Start buttons as executable at the beginning, but then they are greyed out after Start. Stop does the opposite.

I would still appreciate any further advice or comments on executing this GUI, perhaps with improvements, listing any potential problems, etc. Thank you!

DIAG2 := CreateDialog[
   
   Module[
    {buttonpar, resetbutton, startbutton, stopbutton, resetcalc, 
     startcalc, stopcalc, labels, fields, num, numlim, en},
    
    numlim = 100;
    num = 0;
    en = True;
    
    labels = {"num"};
    fields = {InputField[Dynamic[num], Number, Enabled -> False]};
    
    resetcalc[] := (
      num = 0;
      Print["RESET"];
      );
    
    startcalc[] := (
      Print["START"];
      en = False;
      While[num <= numlim, Pause[1]; num = num + 1; num = num++];
      );
    
    stopcalc[] := (
      FrontEndTokenExecute["EvaluatorAbort"];
      en = True;
      Print["STOP"];
      );
    
    buttonpar = {ImageSize -> All, 
      BaseStyle -> {"Evaluate", 12, Bold}};
    
    resetbutton[en_] := 
     Button["RESET", resetcalc[], Evaluate@buttonpar, 
      Method -> "Queued", Enabled -> en];
    startbutton[en_] := 
     Button["START", startcalc[], Evaluate@buttonpar, 
      Method -> "Queued", Enabled -> en];
    stopbutton[en_] := 
     Button["STOP", stopcalc[], Evaluate@buttonpar, 
      Method -> "Preemptive", Enabled -> ! en];
    
    Panel[
     TableForm[{resetbutton[Dynamic[en]], startbutton[Dynamic[en]], 
       stopbutton[Dynamic[en]], labels, fields}]]
    
    ],
   
   Modal -> False, WindowTitle -> "START/STOP TEST"
   
   ];

Best Regards, Ben

$\endgroup$

1 Answer 1

6
$\begingroup$

You could add to your GUI a button like this:

Button[
    "Abort Operation",
    FrontEndTokenExecute["EvaluatorAbort"]
]
$\endgroup$
1
  • $\begingroup$ Thank you, Gustavo! That does what I was looking for. I've rearranged the DIAG2 module such that your Stop, using your example, will stop the increment, and then Start will resume the count. Adding a Reset button can then start the count back at zero. I'll see if I can go from here and develop further for my more complicated notebook ... $\endgroup$
    – BenKolt
    Nov 30, 2022 at 16:37

Your Answer

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

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