MathGroup Archive 2008

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

Search the Archive

Nested DynamicModule and EventHandler

  • To: mathgroup at
  • Subject: [mg92399] Nested DynamicModule and EventHandler
  • From: Hugh Goyder <h.g.d.goyder at>
  • Date: Tue, 30 Sep 2008 07:37:31 -0400 (EDT)

I use modules to localise variables and to simplify code. In
attempting to understand the additional complications of using
DynamicModule in version 6 I am trying to sort out how to work with
buttons, modules and EventHandlers.

Below three of my buttons, with increasing complexity, work but the
fourth, buttonD, fails. I can click points onto the graphic with the
set-up of buttonC but not with buttonD.  I am trying to get
communication between the parent and child modules where points are
introduced into the child module using the EventHandler. Thus with
buttonD pressed clicking on the graphic should produce an additional
primitive in the list maintained by the parent module. What is the
correct way to do this?

Thanks Hugh Goyder

ClearAll[buttonA, buttonB, buttonC, buttonD];
buttonA[] := DynamicModule[{}, Button["button A", active := fa[],
         ImageSize -> Automatic]];
buttonB[] := DynamicModule[{}, Button["button B", active := fb[],
         ImageSize -> Automatic]];
buttonC[] := DynamicModule[{}, Button["button C", active := fc[],
         ImageSize -> Automatic]];
buttonD[] := DynamicModule[{}, Button["button D", active := fd[],
         ImageSize -> Automatic]];

ClearAll[fa, fb, fc, fd];
fa[] := DynamicModule[{}, Plot[x^2, {x, -2, 2}]];
fb[] := DynamicModule[{a = 5}, Column[{Slider[Dynamic[a], {0, 10}],
           Dynamic[Plot[Sin[a*x], {x, 0, 2*Pi}]]}]];
fc[] := DynamicModule[{b = {}, c = {0.5, 0.5}},
       Column[{EventHandler[Dynamic[Graphics[{Line[{{0, 0}, c}], b},
                 PlotRange -> {{0, 1}, {0, 1}}, Frame -> True]],

      "MouseDown" :> (c = MousePosition["Graphics", {}];
        AppendTo[b, Point[c]])],
           Row[{"b ", InputField[Dynamic[b]]}]}]];
fd[] := DynamicModule[{},
     Column[{EventHandler[Dynamic[Graphics[{Circle[c, 0.1], b},
               PlotRange -> {{-1, 1}, {-1, 1}}, Frame -> True]],

     "MouseDown" :> (c = MousePosition["Graphics", {}];
       AppendTo[b, Point[c]])],
         Row[{"b ", InputField[Dynamic[b]]}]}]]

DynamicModule[{active = Graphics[{}], a, g,
  b = {Green, Point[{-0.25, -0.25}]},
     c = {0, 0}},
 Column[{Row[{buttonA[], buttonB[], buttonC[], buttonD[]}],
       Dynamic[active], Row[{"b (outer)", InputField[Dynamic[b]]}],
       Row[{"c ", InputField[Dynamic[c]]}],
       Row[{"active ", InputField[Dynamic[active]]}]}]]

  • Prev by Date: Re: blurry ellipse
  • Next by Date: Re: Launch notebook in FE and evaluate it immediately
  • Previous by thread: Re: Avoiding Null output marker from a function
  • Next by thread: Re: Nested DynamicModule and EventHandler