Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #9267

Re: Input vs. InputField for Interactive Applications

From A Retey <awnl@gmx-topmail.de>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Input vs. InputField for Interactive Applications
Date 2012-03-29 07:57 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <jl14le$l71$1@smc.vnet.net> (permalink)
References <jkunhb$aso$1@smc.vnet.net>

Show all headers | View raw


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

Back to comp.soft-sys.math.mathematica | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Input vs. InputField for Interactive Applications Don <donabc@comcast.net> - 2012-03-28 10:01 +0000
  Re: Input vs. InputField for Interactive Applications A Retey <awnl@gmx-topmail.de> - 2012-03-29 07:57 +0000

csiph-web