In Mathematica I am trying to implement algorithm, for which I would like to have user friendly interface, I have managed to create Panel using Widgets, but when I am trying to provide the value of the textfield with clicking the Submit button, nothing happen.

I have created the following event:

BindEvent["action", Script[buttonClicked[]]

so when the button is clicked buttonClicked[] should be executed, which contains check on the textfield. But nothing happen when clicking Submit. Could you please give me a hand on that.

ref = GUIRun[Widget["Panel",

     "Label", {"text" -> 
       "Please provide the following coefficients:"},
     WidgetLayout -> {"Alignment" -> Center}, Name -> "mainLabel"],

    {Widget["Label", {"text" -> "  Value for p1"}],
     Widget["TextField", {"columns" -> 6}, Name -> "p1_tf"],
     Widget["Label", {"text" -> "            "}]
     Widget["Button", {"text" -> "Submit", 
       BindEvent["action", Script[buttonClicked[]]]}, 
      Name -> button1], WidgetFill[]

ImaginaryQ[expr_] := ! FreeQ[expr, _Complex];

 buttonClicked[] := (If [
     ImaginaryQ@p1_tf == 
      False, {MessageDialog [ 
       "The provided value is not valid. You need to enter complex value"], Exit[]}];)


p[[1]] = p1_tf; (*p1*)  
Print["p1=", p[[1]]];
  • $\begingroup$ @SjoerdC.deVries is this a candidate for migration? Also, the functions GUI* are part of the GUIKit which is still available in v.8. $\endgroup$
    – rcollyer
    May 14 '12 at 14:29
  • $\begingroup$ @rcollyer It's available indeed, but I wouldn't know a good reason to use it. I tried it once, many years ago, to build a progress indicator. Using GUIkit for this goal meant that the progress to be indicated slowed to a crawl. I vaguely recall one other question in this area in one year, so it's apparently not that popular. $\endgroup$ May 14 '12 at 17:39
  • $\begingroup$ @SjoerdC.deVries I have never used it, but I don't build UI's in mathematica, even with Manipulate and Dynamic available. $\endgroup$
    – rcollyer
    May 14 '12 at 17:43
  • $\begingroup$ Hi, I am using Mathematica 7.0 and yes I forgot to include Needs["GUIKit`"]; - this is still available in Mathematica 8 I think.The point is that I need to build UI in Mathematica, common to the one that I have created in the example - to have Panel and in it several TextFields plus 'Submit' button. The point is that I get confused how to call the script to be executed when clicking the Submit button. Help is really appreciate. Thanks! Or to offer me some other solution, that is giving me the same options. $\endgroup$
    – baheca
    May 14 '12 at 20:24
  • $\begingroup$ Could you perhaps implement the needed functionality using Manipulate? Your life would be much easier then. $\endgroup$
    – Ajasja
    May 14 '12 at 21:10

If GUIkit isn't really a prerequisite you could try the standard controls present in Mathematica in all versions as of 6:

   "Please provide the following coefficients:",
   "  Value for p1",
   "            ",
    "Submit", (If[
       ImaginaryQ@p1tf == False, 
         MessageDialog["The provided value is not valid. You need to enter complex value"], 

Mathematica graphics

  • $\begingroup$ I am building some rather complicated interfaces with just the standard MMA functionality. Never needed to use GUiKit. I suggest you follow Sjoerd advice/example $\endgroup$
    – magma
    May 15 '12 at 16:33
  • $\begingroup$ Thanks! That example was really usefull. I will re-write my code. $\endgroup$
    – baheca
    May 17 '12 at 19:17

This works (but feels cumbersome)

p1 = DialogInput[{p1 = 0 + 0 I}, 
   Column[{"Type a value for p1", InputField[Dynamic[p1], String], 
     Button["Proceed", DialogReturn[ToExpression@p1], 
     ImageSize -> Automatic]}]];
If[NumericQ@p1, Print[p1], 
  MessageDialog["You need to enter a complex value for p1."]];

Manipulate could work much better:

Manipulate[p1 = a + b I, {a, 0, 10}, {b, 0, 10}, 
  Button["Run the algorithm!", Print[p1]]]

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.