The Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Math Forum » Discussions » Inactive » comp.soft-sys.math.mathematica

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Help with dynamic functionality
Replies: 2   Last Post: Dec 3, 2012 3:18 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
John Fultz

Posts: 736
Registered: 12/7/04
Re: Help with dynamic functionality
Posted: Dec 3, 2012 3:18 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

There are two problems going on here.

The problem I spotted immediately is that Dynamics are updated based
upon a dependency graph. All variables which are resolved by the
evaluation of the first argument of Dynamic are recorded in this
dependency graph and, if any of them change, the Dynamic is invalidated
and updated. Your Slider depends only upon 'x', and if 'x' isn't
updating, the Slider doesn't update.

So, I immediately thought, let's just add 'enabled' to that first
argument. I.e.,

Slider[Dynamic[enabled; x,

And that didn't work. That gave me some pause. Until I realized the
second problem here. Yes, I had actually gotten the Dynamic to update,
but what I hadn't done was to trigger the second argument. The second
argument only gets triggered when you're actively assigning a value. In
this case, that means only when a user is actually moving the slider.
This is exactly the documented behavior, incidentally; I was just being
a bit thick about realizing this.

Which means that you're going to have to repeat the setting
functionality in the first *and* second arguments. It's got to be in the
first argument because external updates have to set x. It's got to be
in the second argument because the user can still move the mouse, and
you'll need to tell Mathematica how to assign the value to what will now
be a complex first argument.

So something like this works:

Clear[y, SliderModule];
SetAttributes[SliderModule, HoldAll];
SliderModule[enabled_] :=
DynamicModule[{x = 0}, {Slider[
Dynamic[If[! enabled, x = 0]; x, (x = If[enabled, #, 0]) &]],
{Checkbox[Dynamic[y]], Dynamic[y], SliderModule[y]}

John Fultz
User Interface Group
Wolfram Research, Inc.

On Nov 28, 2012, at 2:17 AM, wrote:

> My actual problem is quite involved but I am including a simple
example for
> illustrative purposes. I am trying to reproduce "Enabled"-like
functionality as follows:
> Clear[y, SliderModule];
> SetAttributes[SliderModule, HoldAll];
> SliderModule[enabled_] :=
> DynamicModule[{x = 0}, {Slider[
> Dynamic[x, (x = If[enabled, #, 0]) &]], Dynamic[x]}]
> {Checkbox[Dynamic[y]], Dynamic[y], SliderModule[y]}
> Ideally when the checkbox is checked then the user is able to move the

slider; but when it is unchecked the slider resets to zero. The problem
is when I uncheck the checkbox the slider doesn't reset to zero until I
click on the slider itself. Is there a way to get the slider to
immediately reset when I uncheck the checkbox? Please remember that this
is a simplification of my actual problem so try to keep the SliderModule
structure the same.
> Cheers,
> Sam

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.