3
$\begingroup$

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.

$\endgroup$
11
  • $\begingroup$ Is q2 defined? $\endgroup$
    – cormullion
    Commented Aug 2, 2012 at 9:26
  • $\begingroup$ @cormullion: yes, I have edited in question. $\endgroup$
    – Jennifer
    Commented Aug 2, 2012 at 9:31
  • $\begingroup$ Your code works for me when q2 is defined. $\endgroup$
    – cormullion
    Commented Aug 2, 2012 at 9:32
  • $\begingroup$ @cormullion: How are you making functions in package for buttons? $\endgroup$
    – Jennifer
    Commented Aug 2, 2012 at 9:33
  • 1
    $\begingroup$ 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. $\endgroup$ Commented Aug 2, 2012 at 13:42

1 Answer 1

5
$\begingroup$

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]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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