6
$\begingroup$

I would like to make a panel look like a ChoiceDialog.

Let's say a have a panel like this:

Panel[Column[{Style["What's the gender?", FontFamily -> "Calibri", 
    FontSize -> 14], PopupMenu[Dynamic[x], optionsGender], " ", 
   Style["What's the academic title?", FontFamily -> "Calibri", 
    FontSize -> 14], PopupMenu[Dynamic[y], optionsTitle], 
   Button["Yeah!", Print[x, " ", y]]}]]

After clicking the button, I want to make the whole panel disappear and only leave the print in the output. Does anyone know how to do that? Thx in advance.

$\endgroup$
5
$\begingroup$

This does a targeted overwrite of just the Panel...well, technically, of the DynamicModuleBox with which I'm wrapping the PanelBox:

DynamicModule[{box}, 
 Panel[Column[{Style["What's the gender?", FontFamily -> "Calibri", 
     FontSize -> 14], PopupMenu[Dynamic[x], optionsGender], " ", 
    Style["What's the academic title?", FontFamily -> "Calibri", 
     FontSize -> 14], PopupMenu[Dynamic[y], optionsTitle], 
    Button["Yeah!", NotebookWrite[box, ToBoxes@Row[{x, RawBoxes@" ", y}]]]}]], 
 Initialization :> (box = EvaluationBox[])]

So, the idea is that I'm getting the BoxObject which corresponds to the outer DynamicModuleBox. Then, I can pass that to NotebookWrite. This allows me to overwrite the box regardless of what's inside of, and it works without changing the selection.

It does, however, have the side effect of turning the Output cell into an Input cell (assuming this was in an Output cell to begin with). If that's an issue, then you can add this to the DynamicModule's initialization:

CurrentValue[EvaluationCell[], CellEditDuplicate] = False;
|improve this answer|||||
$\endgroup$
  • $\begingroup$ this is a really useful example for the use of EvaluationBox, which I think I just start to appreciate, so thanks for sharing. One thing I think should be noted is that it won't work with the free CDF Player, most probably because of the NotebookWrite (?). As that use case seems to be closer to Dynamic functionality than to programmatically edit notebooks, do you think there is a chance that WRI will allow NotebookWrite in CDF Player when the first argument is a box in future versions? $\endgroup$ – Albert Retey Mar 18 '16 at 20:23
  • $\begingroup$ @AlbertRetey Probably not since it would be hard to prevent users from creating notebooks using FreePlayer. In this case PaneSelector should be a good enough alternative. $\endgroup$ – Kuba Apr 15 '16 at 17:35
4
$\begingroup$

When clicked, the panel is replaced by the output.

DynamicModule[{output, x, y},
 output = Panel@Column@{
     "What's the gender?",
     PopupMenu[Dynamic[x], {"Male", "Female", Indeterminate}], 
     Spacer@10,
     "What's the academic title?", 
     PopupMenu[Dynamic[y], {None, "MSc", "PhD", All}],
     Button["Yeah!", output = Row@{x, " ", y}]};
 Dynamic@output]
|improve this answer|||||
$\endgroup$
  • $\begingroup$ This is also a nice option for this. Thx! $\endgroup$ – 407PZ Mar 18 '16 at 21:27
2
$\begingroup$

What about a dialog box outside the current notebook?

DialogInput[
 Column[{Style["What's the gender?", FontFamily -> "Calibri", 
    FontSize -> 14], PopupMenu[Dynamic[x], optionsGender], " ", 
   Style["What's the academic title?", FontFamily -> "Calibri", 
    FontSize -> 14], PopupMenu[Dynamic[y], optionsTitle], 
   Button["Yeah!", DialogReturn[Row[{x, " ", y}]]]}]]

This seems to do the same as your panel, except the panel is outside the notebook, and it closes upon the press of the button. See the documentation for DialogInput and DialogReturn.

|improve this answer|||||
$\endgroup$
  • $\begingroup$ I didn't realize that there is a DialogInput. Thanks for the tips. $\endgroup$ – 407PZ Mar 18 '16 at 13:56
2
$\begingroup$

This will do it:

Button["Yeah!", NotebookDelete[EvaluationCell[]]; Print[x, " ", y]]
|improve this answer|||||
$\endgroup$
  • $\begingroup$ +1 Just to spell it out, OP can keep the panel and use this method, by just replacing Print[x, " ", y] in the original code with Print[x, " ", y]; NotebookDelete[EvaluationCell[]] $\endgroup$ – Jason B. Mar 18 '16 at 11:24

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.