1

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

DateTimePopup[] := 
  CreateDialog[
   DynamicModule[{date = {0, 0, 0}, th, tm, ts}, Column[{Row[{
        TextCell["Date:"], Spacer[5], 
        Developer`DateSetter[Dynamic[date]], Spacer[40],
        TextCell["Time:"], Spacer[5], 
        PopupMenu[
         Dynamic[th], # -> "H:" <> ToString[#] & /@ Range[0, 24]],
        PopupMenu[
         Dynamic[tm], # -> "M:" <> ToString[#] & /@ Range[0, 59]],
        PopupMenu[
         Dynamic[ts], # -> "S:" <> ToString[#] & /@ Range[0, 59]]
        }],
      Row[{
        Dynamic[
         DateObject[Sequence@Flatten@Append[date, {th, tm, ts}]]], 
        Spacer[10],
        DefaultButton[
         DialogReturn[
          Dynamic[DateObject[
            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 ?

Kuba
  • 136,707
  • 13
  • 279
  • 740
Neel Basu
  • 961
  • 6
  • 14

1 Answers1

1
  • 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

Kuba
  • 136,707
  • 13
  • 279
  • 740