Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

Needs["GUIKit`"];
ref = GUIRun[Widget["Panel",

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

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

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

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

GUIScreenShot[ref];

p[[1]] = p1_tf; (*p1*)  
Print["p1=", p[[1]]];
share|improve this question

migrated from stackoverflow.com May 14 '12 at 20:19

This question came from our site for professional and enthusiast programmers.

    
@SjoerdC.deVries is this a candidate for migration? Also, the functions GUI* are part of the GUIKit which is still available in v.8. –  rcollyer May 14 '12 at 14:29
    
@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. –  Sjoerd C. de Vries May 14 '12 at 17:39
    
@SjoerdC.deVries I have never used it, but I don't build UI's in mathematica, even with Manipulate and Dynamic available. –  rcollyer May 14 '12 at 17:43
    
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. –  baheca May 14 '12 at 20:24
    
Could you perhaps implement the needed functionality using Manipulate? Your life would be much easier then. –  Ajasja May 14 '12 at 21:10
show 1 more comment

2 Answers

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]]]
share|improve this answer
add comment

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

Panel[
 Column[
  {
   "Please provide the following coefficients:",
   "  Value for p1",
   InputField[Dynamic[p1tf]],
   "            ",
   Button[
    "Submit", (If[
       ImaginaryQ@p1tf == False, 
       {
         MessageDialog["The provided value is not valid. You need to enter complex value"], 
         Exit[]
       }
     ];)]
   }
  ]
 ]

Mathematica graphics

share|improve this answer
    
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 –  magma May 15 '12 at 16:33
    
Thanks! That example was really usefull. I will re-write my code. –  baheca May 17 '12 at 19:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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