0
$\begingroup$

I'm building a dynamic module, but I'm stuck with a few things i don't know how to deal with.

In particular I'm stuck with the "If" sentence and the refresh inside the dynamic module.

Here's a little module I've made with the problems I have:

DynamicModule[{x, button, pop, kindselects, importbutton, file, 
  images}, Dynamic[
  Refresh[Panel@
    Grid[{{Column[{button, kindselects, importbutton, images}, 
        Center]}}, Alignment -> Top], None]],
 Initialization :> (
   pop = 1;
   kindselects = 
    Grid[{{PopupMenu[Dynamic@pop, {1, 2}], 
       "Select which kind of profile you want"}}];

    If[pop == 1, {some instructions}, 
     {some other instructions}];

   importbutton = 
    Button["Image import, choose your image here", {file = 
       SystemDialogInput["FileOpen"], images := Import[file]}];


   )
 ]

This module does two things: imports an image and work on If sentence.

My first problem is with the image. As you can see, I'm importing the image and showing it in a Grid. While to import a first image is not a problem, the second image I import is not showed. In my opinion that's because I have the refresh set to None for this grid. I have it so for other mandatory reasons due to the whole module I'm dealing withbut this is preventing the second image to be shown after being imported.

I'm looking for a way to have some sort of button that, after importing, refreshes the whole thing, but still forcing the None condition in any other case. The mathematica helper this time is not being so useful, and I'm stuck on this.

The second problem is related with the If. I want my module to be such that when I select from a popup menu an option, it does only a prticular kind of calculations, while does other with another option selected. And moreover I'd like it not to proceed with calculations of the non choosen kind, since they would slow down my dynamics.

Unfortunately my coding skill related with dynamic moduleis is at a really early stage, and I'm actually trying to understand how to make the If working inside of it...

I've managed this other little module, but it seems that something's not working:

DynamicModule[{x, button, pop, kindselects,}, Dynamic[
  Refresh[Panel@
    Grid[{{Column[{button, kindselects}, 
        Center]}}, Alignment -> Top], None]],
 Initialization :> (
   pop = 1;
   kindselects = 
    Grid[{{PopupMenu[Dynamic@pop, {1, 2}], 
       "Select which kind of profile you want"}}];
   Dynamic[x[], 
    If[pop == 1, x[] = Table[Table[i, {i, 1, 10}], {j, 1, 10}], 
     x[] = Table[Table[i, {i, 1, 100}], {j, 1, 100}]]];
   button = ButtonBar[{"test x" :> (test = x[])}];


   )
 ]

This should return me a different value of test, whose value is set to the one of x[], when the chosen "pop" value is changed via the popup menù, but it's not working at all....

I hope someone can help me with at least one of the two problems.

Thanks a lot,

Andrea

$\endgroup$
  • 1
    $\begingroup$ Dynamic can be tricky, but simply, they trigger when visible (don't put raw Dynamic lines in Initialization then). p.s. If you want to create some kind of event listener which reacts on PopupMenu setting, take a look at DynamicWrapper. Try to build interface step by step. And read documentation, Initialization is done only when DM cell is first time visible in session so If statement related to variable changed later has not so much sense. $\endgroup$ – Kuba May 7 '15 at 21:55
  • $\begingroup$ @kuba: thanks a lot for your advices, i'm actually learning "on the field", since i started modifing a code of yours actually. (from here: link $\endgroup$ – Andrea May 10 '15 at 8:44
  • $\begingroup$ Great :) Keep in mind that I'm learning too and my old answers are not always good examples of coding. :) $\endgroup$ – Kuba May 10 '15 at 8:50
2
$\begingroup$

I don't know what the popup menu is for -- but I've left that bit of code in there -- but this should be what you need:

DynamicModule[{files, pop, images = ""},

 Column[{
   Row[{"Select which kind of profile you want", 
     PopupMenu[Dynamic@pop, {1, 2}]}, Spacer[3]],
   Button["Image import, choose your image here",
    files = SystemDialogInput["FileOpen",
      {NotebookDirectory[], {"PNG-Files" -> {"*.png"}, 
        "JPEG-Files" -> {"*.jpg"}, "TIFF-Files" -> {"*.tif"}}},
      WindowTitle -> "Import Image"];
    If[files =!= $Canceled && files =!= $Failed,
     images = Import[files],
     images = ""
     ],
    Method -> "Queued"
    ],
   Dynamic@images
   }]
 ]
$\endgroup$
  • $\begingroup$ that's perfect for my uploading needs, thanks! $\endgroup$ – Andrea May 10 '15 at 8:46

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.