       Re: Variable amount of Buttons in Mathematica

• To: mathgroup at smc.vnet.net
• Subject: [mg92803] Re: Variable amount of Buttons in Mathematica
• From: Albert Retey <awnl at gmx-topmail.de>
• Date: Mon, 13 Oct 2008 07:06:15 -0400 (EDT)
• References: <gcscrf\$8l3\$1@smc.vnet.net>

```SDY wrote:
> I'm pretty new to this...
>
> I'm trying to write a function that will create a variable number of buttons (based on some input) and relate each button to a function call with a unique parameter value. For example:
>
> array = {1, 2, 3, 4, 5};
> Column[{Row[
>    Table[Button[array[[n]], Print[n], ImageSize -> {50, 50}], {n, 1,Length[array], 1}]]}]
>
> Gives me five buttons, labeled 1-5, but each prints 'n' when clicked on.
> How would I change this to print the value of n?
>
> My best guess at this point is that there is a scoping issue with n, but I don't have enough Mathematica experience to know how to assign static values...

Button has the attribute HoldRest, to make possible that its argument
contains code that will not be evaluated when the button is created, but
only when it is pressed. So you need to fill in the n in evaluated form,
this is one possibility:

array = {1, 2, 3, 4, 5};
Column[{Row[
Table[With[{n = n},
Button[array[[n]], Print[n], ImageSize -> {50, 50}]], {n, 1,
Length[array], 1}]]}]

hth,

albert

```

• Prev by Date: Exclude O[] from Series[] for Solve[] in Mathematica
• Next by Date: Re: How to print TraditionalForm without evaluation
• Previous by thread: Re: Variable amount of Buttons in Mathematica
• Next by thread: Re: Variable amount of Buttons in Mathematica