# Access local Module variable from Button handler

Here's a simplified piece of code that I'm working with:

Module[{x=42},
Button["Press me", Print[x]]
]


The desired behavior is to make a button which print 42 when user presses it, but it just prints x\$<number> symbol instead.

Update: Looks like it was a bug, i can not reproduce it now. By the way, I am running Mathematica 8.0.4 on Debian Linux. As a workaround solution I used following code:

Module[{x=42},
With[{y=x}, Button["Press me", Print[y]]]
]


From Alexei Boulbitch: I place my comment here, since the domain for answers is closed. I tried the above operator on my machine: PC/XP/Mma 9. It works exactly as described by uranix.

Even more I see the result not in the notebook, but in the Message file. I tried then few other operators just from Menu/Help/Button/Basic Examples and Generalizations and Applications.

I have identified at least two that did not work either, though they should. I know that prom my previous experience. They are:

Button["Click Here", Print[10!]]

Button["Print Date", Print[DateString[]]]


Each of these examples works as follows: after evaluation the button appears, but pressing the button generates results in the Messages, rather than in the notebook.

I attach the image of the notebook with this operators and the message file, as obtained right after the first trial of all the three buttons.

-
its working fine for me..it prints 42 –  Blackbird Aug 29 at 16:16
the same to me. it prints 42 –  Murta Aug 29 at 17:03
Also works for me with V9.0.1 on OS X 10.6.8 –  m_goldberg Aug 29 at 19:20
This question appears to be off-topic because because the question poster's problem can not be reproduced. –  m_goldberg Aug 29 at 19:29
Also prints 42 from version 7 under Windows. –  Mr.Wizard Aug 29 at 19:40