4
$\begingroup$

I am trying to create a popup menu where I can check some boxes to create some plots after the calculation. It is like multiple selections popupmenu. My Frankenstein version, of course, isn't working.

PopupMenu["a", {Row[{"a", Checkbox[Dynamic[fpTFa], {False, True}]}], 
Row[{"b", Checkbox[Dynamic[fpTFb], {False, True}]}], 
Row[{"c", Checkbox[Dynamic[fpTFc], {False, True}]}], 
Row[{"d", Checkbox[Dynamic[fpTFd], {False, True}]}]}]

Any suggestion or solutions? :)

$\endgroup$
  • 1
    $\begingroup$ How wedded are you to this being a PopupMenu? It is possible to do, but requires some cleverness and trickery and will not be entirely robust. A better solution, to my eyes, is to use DialogInput to allow your user to pick their settings and then use Dynamic to display the settings. This affords you greater robustness and control. $\endgroup$ – b3m2a1 Jul 22 '18 at 23:16
  • $\begingroup$ Did any of the answers satisfied your need? There are things to do after your question is answered. It's a good idea to stay vigilant for some time, better approaches may come later improving over previous replies. Experienced users may point alternatives, caveats or limitations. New users should test answers before voting and wait 24 hours before accepting the best one. One weeks is enough wait. Participation is essential for the site, please do your part. $\endgroup$ – rhermans Jul 29 '18 at 9:55
4
$\begingroup$

Here's a little solution using the undocumented AttachCell:

fakePopupMenu1[
   label_,
   HoldPattern[Dynamic[var_, e___]],
    selections_,
   ops : OptionsPattern[]
   ] :=
  DynamicModule[{attached},
   Button[
    PopupMenu[label, {label}, ops],
    If[! MatchQ[attached, _CellObject] || 
      NotebookRead[attached] === $Failed,
     (attached =
       FrontEndExecute@
        FrontEnd`AttachCell[
         EvaluationBox[],
         Cell[
          (* Make selector form *)
          BoxData@
           ToBoxes@
            Panel@
             Grid[
              Append[

               Map[{Row@{#, ": "}, Checkbox[Dynamic[var[#], e]]} &, 
                selections],
               {Null, Null, 
                DefaultButton["Done", NotebookDelete@EvaluationCell[]]}
               ],
              Alignment -> Left
              ],
          Background -> White
          ],
         {Offset[{0, 0}, 0], {Left, Top}},
         {Left, Top},
         "ClosingActions" -> {"OutsideMouseClick"}
         ]
      )
     ],
    Appearance ->
     {
      "Default" -> None,
      "Hover" -> None,
      "Pressed" -> None
      }
    ]
   ];
fakePopupMenu1[
  label_,
   selections_,
  ops : OptionsPattern[]
  ] :=
 DynamicModule[{var = <||>}, 
  fakePopupMenu1[label, Dynamic[var], selections, ops]]

When we use it we get a little popup like this (note that I changed the positioning Top to Bottom for this):

fakePopupMenu1["Choose, yo", Dynamic[var, Echo], {1, 2, 3}]

enter image description here

And since it just takes a Dynamic spec as usual if you click on any of those checkbox boxes it will echo True with the tag var[1] (i.e. it calls f[True, var[sel]] for f in the second argument of Dynamic)

$\endgroup$
0
$\begingroup$

The easiest way to get what you need is to use the following:

TabView[{
  "a" -> Checkbox[Dynamic[fpTFa], {False, True}],
  "b" -> Checkbox[Dynamic[fpTFb], {False, True}], 
  "c" -> Checkbox[Dynamic[fpTFc], {False, True}], 
  "d" -> Checkbox[Dynamic[fpTFd], {False, True}]
  }]

Of course, it is not PopupMenu but it is compact enough and intuitively understandable.

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