Mouseover & Dynamic in Mathematica
Asked Answered
G

5

14

Could anyone point out why this is not working in Mathematica 8:

DynamicModule[{x = Pink},
 Row[
  {Style["Hello", x],
   Mouseover[
    x = Green; "World",
    x = Blue; "World"]}]]

What I expect is to see the color of "Hello" change when I mouse over "World". What I am getting is a pink "Hello" that never changes color.

Grampositive answered 29/11, 2011 at 22:32 Comment(2)
I believe the reason this doesn't work is because MouseOver evaluates the x = ... expressions only once. I don't know enough as to how you can get the behavior you want though.Bael
Wow, I got really good answers from all of you. I up voted you all. Selecting THE answer is really hard. Sjoerd and Mike explained very well why my code fails to work which is what I asked. And Heike, Mr.Wizard and Arnoud proposed how to solve the problem which is what I really wanted to ask. THANK YOU ALL!Grampositive
B
7

A quick check shows that Mouseover evaluates all the expressions inside of it when you first launch it:

Mouseover[Print["One"]; 1, Print["Two"]; 2]

The idiomatic way of actually making the Mouseover modify the values of x is to use MouseAnnotation. Mr. Wizard's answer describes how to achieve this.

Bael answered 29/11, 2011 at 22:52 Comment(0)
Y
10

I think I have waited long enough to be fair. Here is my proposal:

DynamicModule[{x = Pink},
 Row[{
   Dynamic@Style["Hello", If[MouseAnnotation[] === 1, x = Green; Blue, x]],
   Annotation["World", 1, "Mouse"]
 }]
]
Yousuf answered 30/11, 2011 at 1:51 Comment(2)
I would've answered sooner but I had to leave almost immediately after posting my answer. This is the most elegant solution though.Bael
@Mike yours is the answer I would accept were this question mine. It is extremely concise and totally clear. This "answer" doesn't address the OP's question at all; it's quite possible he knew of a method like this before he asked, and was only interested in the behavior of Mouseover.Yousuf
L
9

If you look at the FullForm of the result, you'll see that it only contains the last part of each compound instruction set. Apparently Mouseover evaluates its arguments and only stores the results.

enter image description here

Lisp answered 29/11, 2011 at 22:51 Comment(0)
L
9

Try using EventHandler with "MouseEntered" and "MouseExited":

DynamicModule[{c = Pink}, Row[{
 Style["Hello", FontColor -> Dynamic[c]], 
 EventHandler[
  "World", {
   "MouseEntered" :> (c = Blue), 
   "MouseExited" :> (c = Green)
}]}]]
Lutz answered 30/11, 2011 at 1:51 Comment(6)
Interesting. Is there a reason you don't implement the example with this method?Yousuf
@Yousuf -- You mean why isn't Mouseover behaving like EventHandler above? I'd have to check.Lutz
No, I think Mike's answer shows the behavior of Mouseover quite well. I just meant is there a reason you did not use this method to write code that produces the result that mine does, which is what I believe the OP was trying to achieve?Yousuf
I thought it was more illustrative to EventHandler's behavior. But I changed it to act like the original example.Lutz
Please do as you think best, but that seems more clear to me. However, this doesn't appear to be working; is this v8 only?Yousuf
@Yousuf -- Yes, this is new in V8 (but now that I look at it, apparently not documented under EventHandler).Lutz
B
7

A quick check shows that Mouseover evaluates all the expressions inside of it when you first launch it:

Mouseover[Print["One"]; 1, Print["Two"]; 2]

The idiomatic way of actually making the Mouseover modify the values of x is to use MouseAnnotation. Mr. Wizard's answer describes how to achieve this.

Bael answered 29/11, 2011 at 22:52 Comment(0)
B
7

As an alternative you could do something like

DynamicModule[{col = Pink}, 
 Row[{Style["Hello ", FontColor -> Dynamic[col]], 
   Dynamic@If[CurrentValue["MouseOver"],
     col = Green; "World", 
     col = col /. Green -> Blue; "World"]}]
] 
Baskett answered 29/11, 2011 at 22:54 Comment(1)
@Mr.Wizard: Yes I know. I realized this later but I hoped nobody would notice until I got a chance to fix it (which I have now).Baskett

© 2022 - 2024 — McMap. All rights reserved.