MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: How to make a Button inside Manipulate?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg81081] Re: How to make a Button inside Manipulate?
  • From: Jon McLoone <jonm at wolfram.co.uk>
  • Date: Mon, 10 Sep 2007 19:01:01 -0400 (EDT)
  • References: <fc0i23$c32$1@smc.vnet.net>

On Sep 9, 11:31 am, "Nasser Abbasi" <n... at 12000.org> wrote:
> "Nasser Abbasi" <n... at 12000.org> wrote in message news:...
> > The basic problem I have is that I want to have a button, in which it is
> > in a fixed location, and only when clicked will the Manipulate
> > 'expression' gets evaluated.  The reason I want to do this, is that now I
> > have number of controls, and I do not want the function called everytime I
> > adjust one control, because I might have few to adjust before I am ready.
> > So I want a way to adjust/set the control to some values, and only then
> > tell manipulate to call the function with the current set of values in the
> > control.
>
> > So, I figured I make a button, which I click on when I am ready to do
> > this.
>
> > This is the first solution I came up with, using a flag
>
> > ---------------- code------
> > Remove["Global`*"]
> > flag = True;
> > foo[x_] := Text[x]
> > res = 0;
> > m = Manipulate[If[flag, {flag = False; res = foo[x]}, res], {x, 0, 10}];
> > Column[{m, Button["Press ...", {flag = True; m}]}]
> > ----- end code ------------------
>
> > The problem with the above is that the button is floating. So if foo[]
> > generates large output, the button will move lower. I wanted the button to
> > be fixed, above the controls themselves used by Manipulate.
>
> > It sounds simple, but I have no idea how to do it. I looked at 2 examples
> > in demonstration which uses buttons, but the code is too complicated for
> > me to see the pattern.
>
> > I tried this:
>
> > foo[x_] := Module[{}, Print["in foo x=", x]; Text[x]]
>
> > Manipulate[ Button["press to call foo...", foo[x]], {x, 0, 1}]
>
> > The problem with the above is that the button goes in the place where the
> > output from foo[] is supposed to go. I know foo[] is getting called OK,
> > with the updated x values, but the button is in the way for the any
> > graphics output to be displayed from foo[].
>
> > is there a simple solution to this?
>
> > thanks,
> > Nasser
>
> I found a way! The trick was to use a Button as a pure function inside
> Manipulate
>
> foo[x_] := Text[x]
>
> Manipulate[If[doIt, {doIt = False; res = foo[x]}, res], {x, 0, 1},
>   {{doIt, True, ""}, Button["RUN", doIt = True] & }
> ]
>
> I am a happy man now, I got my button where I want it :)
>
> Nasser

The Button has access to the Manipulate variable, so you can put the
code there rather than having to put it in the first argument of the
Manupulate held with your doIt flag trick.

eg

DynamicModule[{currentvalue = ""},
 Manipulate[currentvalue,
  {x, 0, 1},
  {{doIt, True, ""}, Button["RUN", currentvalue = foo[x]] &}]]

Or slightly neater but equivalent...

Manipulate[currentvalue,
 {x, 0, 1},
 {{doIt, True, ""}, Button["RUN", currentvalue = foo[x]] &},
 {currentvalue, "", ControlType -> None}]

Jon McLoone
http://members.wolfram.com/jonm




  • Prev by Date: Re: How to make a Button inside Manipulate?
  • Next by Date: Re: Problem with inverse laplace transform
  • Previous by thread: Re: How to make a Button inside Manipulate?
  • Next by thread: Re: MathML applications