Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have created the following simple GUI:

{num10, num20, path0, date0} =
  DialogInput[
   {num1 = 100, num2 = 10, path = "a path", date = "11/11/2012"},
   Column[{
     "First number", InputField[Dynamic[num1], Number],
     "Second number", InputField[Dynamic[num2], Number],
     "Path of InputData", InputField[Dynamic[path], String],
     "Type in a date (DD/MM/YYYY)", 
     InputField[Dynamic[date], String],
     Button["Proceed", DialogReturn[res = {num1, num2, path, date}], 
      ImageSize -> Automatic]}]];

After filling in the required information and clicking the "Proceed"-button a simple code/calculation follows:

{num10 + num20, path0, AbsoluteTime[{date0, {"Day", "/", "Month", "/", "Year"}}]}

Here is what I would like to do:

  1. Displaying only GUI: I would like to create a file "MyProgram" which - when opened - pops up immediately the GUI (the notebook will be opened in the background for further calculations but not displayed to the user).

  2. Drag & Drop: In the third field I ask for a path for input data. Is it possible to insert the path by dragging the file (in this case a xls-file) and drop it into the field inserting its path?

  3. Displaying results in GUI: After clicking the button "Proceed" I would like to display the results in the same GUI, below the input fields.

  4. Info button For the fourth field I added information on the format of the date. It would look much nicer when this information would pop up when clicking on a "?"-button. How can I implement this button?

share|improve this question
    
did you find the solution about drag and drop...? –  subbu Feb 8 '13 at 5:51
add comment

2 Answers 2

up vote 8 down vote accepted

AFAIK there is no possiblity to react to the OS drag & drop, but I would'nt be surprised if someone has found a way to get that work. Anyway, the following is roughly what I'd do:

guidefinition = Deploy@Panel@DynamicModule[{
     num1 = 100, num2 = 10, 
     path = FileNameJoin[{$HomeDirectory, "Desktop", "file.xls"}],
     year = Null, month = Null, day = Null, date = Null, res = ""
     },
    Column[{
      "First number",
      InputField[Dynamic[num1], Number],
      "Second number",
      InputField[Dynamic[num2], Number],
      "InputData",
      Row[{
        InputField[Dynamic[path], String, Enabled -> False],
        FileNameSetter[Dynamic[path], 
         "Open", {"Excel Files" -> {"*.xls"}}]
        }],
      "Date",
      Row[{
        InputField[
         Dynamic[day, (day = #; 
            date = date2strng[year, month, day]) &],
         Number, FieldSize -> 2, Alignment -> Right, 
         FieldHint -> "DD"],
        "/",
        InputField[
         Dynamic[month, (month = #; 
            date = date2strng[year, month, day]) &],
         Number, FieldSize -> 2, Alignment -> Right, 
         FieldHint -> "MM"],
        "/",
        InputField[
         Dynamic[year, (year = #; 
            date = date2strng[year, month, day]) &], Number, 
         FieldSize -> 4, Alignment -> Left, FieldHint -> "YYYY"]
        }],
      Button["Proceed", res = compute[num1, num2, path, date], 
       ImageSize -> Automatic,Method->"Queued"],
      Dynamic[res]
      }
     ],
    Initialization :> (
      date2strng = Function[
        If[FreeQ[{##}, Null],
         StringJoin[
          IntegerString[#3, 10, 2], "/", IntegerString[#2, 10, 2], 
          "/", IntegerString[#1, 10, 4]
          ],
         Null
         ]
        ];
      compute[num1_, num2_, path_, date_] := {num1, num2, path, date};
      )
    ]

once the gui looks and behaves as desired you can do:

CDFDeploy[
 FileNameJoin[{$HomeDirectory, "Desktop", "simplegui.cdf"}],
 guidefinition,
 WindowSize -> {500, 300}
 ]

It will:

  1. Display only GUI by creating a DynamicModule embedded in a CDF. All your code should go into the Initialization option. This way there is no need for "another" notebook, as the one (the cdf) you create contains all the code. Open the cdf and the gui is ready to work (depending on where you save it, you will need to accept to allow dynamic evaluation of "potential unsafe content").
  2. Input filename via FileSetter which opens the system file opener dialog, preset to only accept xls files. Not exactly drag and drop but probably the most convenient way to get an existing file name that can be done easily with the documented gui features of Mathematica.
  3. Use Dynamic[res] to show the results, which will be automatically updated when the "Proceed" button is pressed.
  4. Use a single InputField for day, month and year, each with a FieldHint option to show the information about how to enter the date. The date string is then automatically created from those inputs whenever one of them changes.

You could of course use the FieldHint option also with only one string inputfield for the date, but with the above you have already some additional "checks" for valid input. To be on the save side I would probably combine these features with appropriate input checks as Istvan has shown.

CDFDeploy only is available for Mathematica 8.0.4, if you use older versions or prefer to create a notebook instead of a CDF-document, you could replace the CDFDeploy with something like:

nb = CreateDocument[{guidefinition}, WindowSize -> {500, 300}];
NotebookSave[nb,FileNameJoin[{$HomeDirectory, "Desktop", "simplegui.nb"}]]

This is not directly related to your questions, but I think you should also be aware of the restriction that a CDF-document "played" with the free CDF-Player will AFAIK not allow to import data from local files. It should work alright if you open the CDF-document with Mathematica or Player Pro, though.

share|improve this answer
    
@AlberRetey: 1. I copied your code into a notebook and evaluated it. No cdf-file has been created. 2. Nice approach with FileSetter. 4. Nice approach with InputField too. –  John Jun 29 '12 at 15:04
    
@John: which version do you use? I forgot to mention that CDFDeploy only was introduced with version 8.0.4 ... –  Albert Retey Jun 29 '12 at 15:14
    
Damn it. I run version 8.0.1. Thx for the edit. –  John Jun 29 '12 at 15:42
    
I'm getting close. But: How do I get the output, which is generated below the button, available for copy-paste. –  John Jun 29 '12 at 17:02
    
You could use something like: Style[Dynamic[res], Deployed -> False] which will let you select the result and copy it. Maybe wrapping Style[...] with Framed oder Panel will it make somewhat more clear to the user where she can select. –  Albert Retey Jun 30 '12 at 10:50
add comment

I think these questions being indeed basic, are rather important. I cannot answer the first two myself, though would love to see some methods that can deal with them. As always, there are other ways to solve the remaining two questions.

  1. Since Mathematica is not designed to produce standalone ("executable") files, you need to have either Mathematica or the Wolfram Player (or PlayerPro) to run any Mathematica notebook. Furthermore, we are dealing here with dialogs and as far as I know it is impossible to create a distributable CDF document that has dialogs in it (it is official, see here). The most that you can do is to hide the source notebook during evaluation: as you run the code, the appearing dialog immediately makes the source notebook disappear (Visible->False). When the dialog is closed in any way, the visibility is reset.
  2. I don't know how to handle OS drag & drop, maybe someone else has an idea.
  3. According to the restrictions you set up, proceeding is only sensible if the four variables have correct values (number, number, string, string). I've used the ContinuousAction option for the InputFields to register any values immediately so that the Proceed button becomes enabled when the values are correct.
  4. Info button here is an ActionMenu, which is here abused to hold any "About"-text, as it allows for a larger body of text to appear in its list argumetn without taking up lots of space in the gui, or without calling for other dialog windows.

Let's see the code (apparently, code blocks don't format correctly, if there is no extra text below a numbered list...):

nb = EvaluationNotebook[];
DynamicModule[{num1, num2, date, path, res = ""},
  SetOptions[nb, Visible -> False];
  CreateDialog[
   Column[{
     "First number", 
     InputField[Dynamic[num1], Number, ImageSize -> 200, ContinuousAction -> True],
     "Second number", 
     InputField[Dynamic[num2], Number, ImageSize -> 200, ContinuousAction -> True],
     "Path of InputData", 
     InputField[Dynamic[path], String, ImageSize -> 200, ContinuousAction -> True],
     "Type in a date (DD/MM/YYYY)", 
     InputField[Dynamic[date], String, ImageSize -> 200, ContinuousAction -> True],
     "Result",
     Panel[Dynamic@res, ImageSize -> {200, 40}],
     Grid@{{
        Button["Proceed", res = {num1, num2, path, AbsoluteTime[{date, {"Day", "/", "Month", "/", "Year"}}]}, 
         ImageSize -> Automatic, 
         Enabled -> 
          Dynamic[NumberQ@num1 \[And] NumberQ@num2 \[And] 
            StringQ@path \[And] StringQ@date]],
        Button["Cancel", SetOptions[nb, Visible -> True]; 
         DialogReturn[], ImageSize -> Automatic],
        ActionMenu["?", {"Long text about who made this program code, when,\n\
what is the version number and how to use it..."}]
        }}}],
   NotebookEventActions :> {"WindowClose" :> (SetOptions[nb, 
        Visible -> True])}]];

Mathematica graphics

share|improve this answer
    
+1 for a very neat solution! –  R Hall Jun 30 '12 at 22:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.