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 am trying to make a Package for a piece of code though it's not needed here as it is very small. But my actual code here is a little bit different from that code. When I tried to make a package for Button. I found that the symbol True is protected.

I have made a package with the function Buttonfun

 BeginPackage["package`"]
 Buttonfun[g1_,g2_]:= Button["     ",(g1=!g1;),Background->g2,Appearance->          
                     "Frameless",Method-> "Queued"];
 EndPackage[]

but when I call this with

  Buttonfun[q1,Orange]

from my notebook, it is showing error message symbol True is protected.

share|improve this question
    
Is q2 defined? –  cormullion Aug 2 '12 at 9:26
    
@cormullion: yes, I have edited in question. –  Jennifer Aug 2 '12 at 9:31
    
Your code works for me when q2 is defined. –  cormullion Aug 2 '12 at 9:32
    
@cormullion: How are you making functions in package for buttons? –  Jennifer Aug 2 '12 at 9:33
1  
As I understand it now, the first (large) block of code isn't really necessary to understand your problem, nor is it actually a package problem. Only the Buttonfun definition and call are necessary. If you agree, you could cut your question down to these essentials. –  Sjoerd C. de Vries Aug 2 '12 at 13:42

1 Answer 1

up vote 5 down vote accepted

You need to prevent q1 from being immediately evaluated when you use it in Buttonfun[q1,Orange], otherwise Buttonfun will try to make the assigment True = !True which causes the error. To do this you have to tell Buttonfun to "hold" its first argument, so it can make the assigment to the symbol q1 instead of the value True.

The code to tell Buttonfun to hold its first argument is:

SetAttributes[Buttonfun,HoldFirst]
share|improve this answer

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.