Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I need to have 100 buttons, which their color is defined by the parity of number of times they are pressed: If they're pressed an even number of times their color is blue, and otherwise red. I don't know how to use 'Button' and 'Dynamic' to do so.

Thanks in advance.

share|improve this question

closed as off-topic by belisarius, Michael E2, Kuba, Pickett, Artes Nov 7 '13 at 22:48

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – belisarius, Michael E2, Kuba, Pickett, Artes
If this question can be reworded to fit the rules in the help center, please edit the question.

3  
Example right from the help DynamicModule[{x = 0}, Button["xxx", x = Mod[x + 1/10, 1], Background -> Dynamic[Hue[x]]]] Voting to close –  belisarius Nov 7 '13 at 22:03
1  
To add to what belisarius said, you just need to change the x = Mod[...] line to return Blue if x is Red and vice versa. That automatically takes care of the even/odd. Also, remove the Hue[]. –  rm -rf Nov 7 '13 at 22:07
    
@belisarius As a matter of fact, I had found another example from the help before my post, but I couldn't change it so that the red/blue colors work. –  Behzad Nov 7 '13 at 22:11
2  
I don't remember where but there is nice way shown in documentation with ReplaceAll: DynamicModule[{bg = Red}, Button["xxx", bg = bg /. {Red -> Blue, Blue -> Red}, Background -> Dynamic[bg]]] –  Kuba Nov 7 '13 at 22:13
    
@Behzad The colors problem is easy (you got three comments about that instantly) the hard part is doing that for 100 buttons. But you're not still facing it –  belisarius Nov 7 '13 at 22:20

1 Answer 1

Done!

Table[DynamicModule[{x = 0}, 
   Button[i + 10 j, x = Mod[x + .5, 1], 
    Background -> Dynamic[Hue[x]]]], {j, 0, 9}, {i, 1, 10}] // Grid
share|improve this answer
    
That's the way. Well done :) –  belisarius Nov 7 '13 at 22:41
    
@belisarius I was editing that:( –  Alex Nov 7 '13 at 22:43
    
afarin behzad!!! –  Alex Nov 7 '13 at 22:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.