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.

Can someone clarify how to perform a DialogInput operation within a Button? I wrote some code for that but it's not working.

Button["Enter value", 
 DialogInput[{name = ""}, 
  Column[{InputField[Dynamic[name], String], 
    Button["Hi", Dynamic[name]]}]]]

My problem is this — when I run the above code, the given text is displayed but when I click on the button, the actual operation is not being carried out.

share|improve this question
3  
Please make a better effort to use proper capitalization and fewer ellipsis........ –  rm -rf Jul 2 '12 at 18:35
    
But, the above DialogInput works as you expect and want when run in the notebook and not from a button? –  Rojo Jul 2 '12 at 18:38
    
I mean, the button issue is probably fixed by adding Method->"Queued" to the button, but even so, is that your only problem? –  Rojo Jul 2 '12 at 18:53
1  
What is the "Hi" button supposed to do? –  Matariki Jul 2 '12 at 21:36
    
What is the meaning of Method-> "Queued" ?? –  subbu Jul 5 '12 at 5:34
add comment

1 Answer

As Rojo has pointed out, Method -> "Queued" can be used for the Button to wait for the dialog to appear, be evaluated, and return. I assume you want to use the value of name in some outer computation, so I forwarded it via a DialogReturn and therefore it is made global (while name inside DialogInput is local). Note that DialogReturn is the standard way to return values from a dialog.

Dynamic@output

Button["Enter value", 
 output = DialogInput[{name = ""}, 
   Column[{InputField[Dynamic[name], String], 
     Button["Accept", DialogReturn[name]], 
     Button["Reject", DialogReturn[]]}]], Method -> "Queued"]

Mathematica graphics

Accepting the input text updates the value of output in the first line (Out[1]).

share|improve this answer
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.