4
$\begingroup$

I'm trying to implement a button that can show and hide output text in a panel/tab, but I can't find a specific command to do that. I tried StringReplace, but it doesn't work for me.

$\endgroup$
0
4
$\begingroup$
showhide = Transpose@{#, ToExpression@#}&@{"Red", "Transparent"};
i = 1;
f = Mod[#, 2] &;
st = Blue;


Panel[Column@{Row@{Slider[Dynamic@x], Dynamic@Style[x, st]},
              Button["Set to " Dynamic@ToString@showhide[[1 + f@i, 1]], 
                     st = showhide[[1 + f[i++], 2]]]}]

Mathematica graphics

$\endgroup$
3
$\begingroup$

I really think that you should give more details about what you are looking for. As it is I had do a fair amount of guessing. Nevertheless, here is a demonstration that might give you ideas of how something like you are considering can be done

SeedRandom[111];
With[{
  words = "grumpy wizards make a toxic brew for the jovial queen" // StringSplit},
  With[{n = Length @ words},
    DynamicModule[{redness, reds, r = 1, blueness, blues, b = 1}, 
      reds = MapThread[{redness, Text[#1, #2]} &, {words, RandomReal[1., {n, 2}]}];
      blues = MapThread[{blueness, Text[#1, #2]} &, {words, RandomReal[1., {n, 2}]}];
      Dynamic @ Column[{
        Graphics[{reds, blues},
          Frame -> True,
          ImageSize -> Medium],
        Row[{
          Button[{"Show Red", "Hide Red"}[[Mod[r, 2] + 1]], 
            redness = {Red, Transparent}[[Mod[r++, 2] + 1]]],
          "  ",
          Button[{"Show Blue", "Hide Blue"}[[Mod[b, 2] + 1]], 
            blueness = {Blue, Transparent}[[Mod[b++, 2] + 1]]]}]},
      Alignment -> Right],
      Initialization :> (redness = Red; blueness = Blue;)]]]

This code could be made considerably more compact, but doing so would probably would make it less clear as a tutorial.

Initial display:

both

Hide Blue button clicked on:

red

$\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.