Re: Input vs. InputField for Interactive Applications
- To: mathgroup at smc.vnet.net
- Subject: [mg125686] Re: Input vs. InputField for Interactive Applications
- From: A Retey <awnl at gmx-topmail.de>
- Date: Thu, 29 Mar 2012 02:56:27 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <jkunhb$aso$1@smc.vnet.net>
Hi, > At a place inside a function, I am asking the user > to input the lower case letter a (for automatic) or > a series of integers (representing months from Jan. to Dec. > The integer numbers can be separated by commas AND/OR spaces) . > > After the user has input either the lower case a > OR a series of integers in the range 1 to 12 inclusive, > the function takes this input and performs certain > actions based upon the input from the user. > > That's it ! That's the whole problem. > > I have two choices with Mathmatica, it appears, > to do the simple task outlined above: > > (1) I can use the Input function. The trouble with this is that > a series of integers looks like a multiplicaiton to Mathematica > and it wants to multiply the integer numbers - clearly not > what I need as input. > > (2) I can use the InputField function. The troulbe with this > is that it puts out an input field, but then doesn't wait > for the user to input the list of integers. It immediately > goes to the next line of code and executes that, and so on. > > I need a hybrid of the Input and InputField functions. You need a custom "modal" dialog, which can be programmed with DialogInput. Here is one way, which still misses some additional checks which are due before calling ToExpression on just "something" the user types :-). Also not that DialogInput (just as Input) can eventually return $Canceled, when the window is closed without clicking the OK button... test[] := Module[{response, values}, response = DialogInput[{ DynamicModule[{str = ""}, Column[{ InputField[Dynamic[str], String], DefaultButton["OK", DialogReturn[str]] }] ] }]; Switch[response, "a", values = Automatic, _String, values = ToExpression[StringSplit[response, (Whitespace | ",") ..]], _, values = {}; ] ] You might want to have a look at this: tutorial/CreatingDialogBoxes. I also think you should have a look at tutorial/IntroductionToDynamic and maybe tutorial/AdvancedDynamicFunctionality if you need further insight. Another side note: I would strongly recommend to separate the function which does the calculation from the interaction with the user, e.g. by providing the result of the above function as an input to the calculating function or call the above from the calculating function only when the input wasn't given as an argument. There are various good reason to do that, but most obviously you'll start to hate the dialog once you are testing and debugging the actual calculation... hth, albert