Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 Dr. belisarius, Michael E2, Kuba, C. E., 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." – Dr. belisarius, Michael E2, Kuba, C. E., Artes
If this question can be reworded to fit the rules in the help center, please edit the question.

Example right from the help DynamicModule[{x = 0}, Button["xxx", x = Mod[x + 1/10, 1], Background -> Dynamic[Hue[x]]]] Voting to close – Dr. belisarius Nov 7 '13 at 22:03
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[]. – R. M. 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
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 – Dr. belisarius Nov 7 '13 at 22:20


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 :) – Dr. 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.