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.

When I code in Mathematica, I often find myself having to repeat large chunks of code for a function to work. The latest example of this is a color changing button that prints when clicked, code shown below.

Mouseover[
 Style[
  Button[
   Pane["Print", FrameMargins -> {{4, 4}, {4, 4}}, 
    ImageSize -> {34, 24}],
   Print["Print"],
   Appearance -> None
   ], Background -> Red
  ],
 Style[
  Button[
   Pane["Print", FrameMargins -> {{4, 4}, {4, 4}}, 
    ImageSize -> {34, 24}],
   Print["Print"],
   Appearance -> None
   ], Background -> Green
  ]
 ]

The only thing that is changing on the mouse over is the button color, but for this to work I need to repeat a whole chunk of code.

A simpler way of doing this is to define a button color function, and then use that in the Mouseover function, code shown below.

ButtonColor[color_] :=
 Style[
  Button[
   Pane["Print", FrameMargins -> {{4, 4}, {4, 4}}, 
    ImageSize -> {34, 24}],
   Print["Print"],
   Appearance -> None
   ], Background -> color
  ]

Mouseover[
 ButtonColor[Red],
 ButtonColor[Green]
 ]

This is better, but what if i only have to refer to the button color changing function for one mouse over button? Should I really have to define a function to use it for one instance?

I think the ideal way to code this would be to use Table or something similar, code shown below.

Mouseover[
 Table[
  Style[
   Button[
    Pane["Print", FrameMargins -> {{4, 4}, {4, 4}}, 
     ImageSize -> {34, 24}],
    Print["Print"],
    Appearance -> None
    ], Background -> color
   ],
  {color, {Red, Green}}
  ]
 ]

The only problem, Table doesn't work because it creates EXTRA BRACES!

Is there a better way of coding this color changing button?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

You don't have repeat yourself. You can map a pure function defining the button over a list of the background colors and then apply Mouseover Like so:

Mouseover @@ 
  (Button[Panel["Print", FrameMargins -> {{4, 4}, {4, 4}}, 
     Background -> #], Print["Print"], Appearance -> None] & /@ {Red, Green})

button

share|improve this answer
    
Oohh, this is exactly what I was after, I think. Thanks for the quick reply ^_^ –  user12572 May 18 at 2:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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