1
$\begingroup$

I have a graphic, with on the left a PopupMenu, a Rectangular, a Text and a Button.

If the value of the PopupMenu is right/wrong, after pressing the button, the Rectangular become green/red and the Text become right/wrong.

Labeled[listar1 = {PopupMenu[Dynamic[r1], {"Sen","Cos","Tan"}]};
         Plot[Sin[x],{x, -2 Pi, 2 Pi}, PlotStyle->Red,PlotRange->{-4,4},ImageSize->Large],
         Column[Flatten[{{Text["What function?"],listar1},
         Graphics[{Dynamic[If[r2,Green,Red]], Rectangle[{0, 0}]},ImageSize->{20,20}],
         Graphics[Text[Style[Dynamic[If[r2,"Right!","Wrong!"]],Medium]],ImageSize->{100,30}],
         Button["Check",r2=(r1==="Sen")]}]],Right];

How can I set the default color of the Rectangular (Blue for example)?

And also, how can I hide the Text at the beginning with no string showed?

$\endgroup$
  • 1
    $\begingroup$ If takes a third argument for neither true nor false. Use that. e.g. If[r2, Green, Red, Blue] and If[r2, "Right!", "Wrong!", ""]. $\endgroup$ – b3m2a1 May 25 '17 at 16:59
  • $\begingroup$ @MB1965 It doesn't work: the rectangular has still the last color and the text also. There is only a new empty entry in the popupmenu $\endgroup$ – Ale1794 May 25 '17 at 17:04
  • $\begingroup$ Where are you using that? Are you adding the third argument to the If calls you have? I should note it works for me (just tried it). $\endgroup$ – b3m2a1 May 25 '17 at 17:05
  • $\begingroup$ @MB1965 Labeled[listar1 = {PopupMenu[Dynamic[r1], {"Sen","Cos","Tan"}]}; Plot[Sin[x],{x, -2 Pi, 2 Pi}, PlotStyle->Red,PlotRange->{-4,4},ImageSize->Large], Column[Flatten[{{Text["What function?"],listar1}, Graphics[{Dynamic[If[r2,Green,Red,Blue]], Rectangle[{0, 0}]},ImageSize->{20,20}], Graphics[Text[Style[Dynamic[If[r2,"Right!","Wrong!",""]],Medium]],ImageSize->{100,30}], Button["Check",r2=(r1==="Sen")]}]],Right]; $\endgroup$ – Ale1794 May 25 '17 at 17:07
  • $\begingroup$ Also if you're adding the text outside of the Graphics you don't need a Text and Graphics wrapper on it. $\endgroup$ – b3m2a1 May 25 '17 at 17:07
4
$\begingroup$

Give this a try. I wrapped it in DynamicModule to localize r1 and r2:

DynamicModule[{r1, r2},
 Labeled[
  listar1 = {PopupMenu[Dynamic[r1], {"Sen", "Cos", "Tan"}]};
  Plot[Sin[x],
   {x, -2 Pi, 2 Pi},
   PlotStyle -> Red, PlotRange -> {-4, 4},
   ImageSize -> Large
   ], Column[
   Flatten[{{Text["What function?"], listar1}, 
     Graphics[{Dynamic[If[r2, Green, Red, Blue]], Rectangle[{0, 0}]}, 
      ImageSize -> {20, 20}],
     Dynamic[If[r2, "Right!", "Wrong!", ""]],
     Button["Check", r2 = (r1 === "Sen")]}]
   ],
  Right]
 ]

Before: before

After: after

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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