I am trying to create a DateTime input dialog that will return a DateTimeObject

DateTimePopup[] := 
   DynamicModule[{date = {0, 0, 0}, th, tm, ts}, Column[{Row[{
        TextCell["Date:"], Spacer[5], 
        Developer`DateSetter[Dynamic[date]], Spacer[40],
        TextCell["Time:"], Spacer[5], 
         Dynamic[th], # -> "H:" <> ToString[#] & /@ Range[0, 24]],
         Dynamic[tm], # -> "M:" <> ToString[#] & /@ Range[0, 59]],
         Dynamic[ts], # -> "S:" <> ToString[#] & /@ Range[0, 59]]
         DateObject[Sequence@Flatten@Append[date, {th, tm, ts}]]], 
            Sequence@Flatten@Append[date, {th, tm, ts}]]]]]
      }]], WindowSize -> {400, 200}];

When I try to use that function I get a popup but I expected the expression to evaluate after I DialogReturn

dt = DateTimePopup[]
 NotebookObject[FrontEndObject[LinkObject["dtt6m_shm", 3, 1]], 46]

But I get a return immediately. How can I get the date time object returned from the dialog ?

  • $\begingroup$ Use DialogInput and drop Dynamic from DialogReturn. $\endgroup$
    – Kuba
    Apr 28 '17 at 12:25
  • If you want a dialog to return a value for a purpose of currently running evaluation you need to use one of kernel blocking dialogs:

    DialogInput, ChoiceDialog, Input
  • Otherwise CreateDialog and friends will just popup a window but the current evalaution will proceed without caring about it. Of course you can put DefaultButton[myVar=15;DialogReturn[]] and myVar will get that value once OK is clicked but it will be asynchronous with respect to the main evaluation.

It is up to you what to use, each can be useful. E.g. Cloud login dialog is a kernel blocking one since subsequent evaluation needs those credentials while Preferences dialog is not because it does not matter when will you change some FE settings.

The first method requires you to change CreateDialog, the latter to pass the variable. I suppose you want the former.

Additionaly, drop Dynamic from DialogReturn, otherwise you will get Dynamic[expression] and will run into the problems like OP there: programming-with-dynamic


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.