Re: Button[label, action] - InputString[]?
- To: mathgroup at smc.vnet.net
- Subject: [mg83624] Re: Button[label, action] - InputString[]?
- From: Albert <awnl at arcor.net>
- Date: Sun, 25 Nov 2007 04:35:21 -0500 (EST)
- References: <fi8pp9$h2g$1@smc.vnet.net>
Joe Sneed wrote: > I run V6.01 under Windows XP > > I want to construct a button that reads a string input and writes > something determined by the input to a notebook. When I click on "close" > of "X" in the input dialog box, I want nothing to appear in the notebook. > > I have two questions: one dealing with evaluation method; another > dealing with the use of If[ _, _, _] in the button function. > > As a test, I try to read the input and write it to a selected notebook using > > Button[Style["LABEL", Black, 10, FontFamily -> "Arial Black"], > tag=InputString[]; > nb=SelectedNotebook[]; > If[tag ? $Canceled, > NotebookWrite[nb,Cell[tag,"Text"]]]] > > to construct the button. > > This yields a button whose underlying expression is > > Cell[BoxData[ > > ButtonBox[ > StyleBox["\<\"LABEL\"\>", > StripOnInput->False, > FrontFaceColor->GrayLevel[0], > BackFaceColor->GrayLevel[0], > GraphicsColor->GrayLevel[0], > FontFamily->"Arial Black", > FontSize->10, > FontColor->GrayLevel[0]], > Appearance->Automatic, > ButtonFrame->"DialogBox", > ButtonFunction:>($CellContext`tag = > InputString[]; $CellContext`nb = > SelectedNotebook[]; > If[$CellContext`tag != $Canceled, > NotebookWrite[$CellContext`nb, > Cell[$CellContext`tag, "Text"]]]), > Evaluator->Automatic, > Method->"Preemptive"]], "Output", > CellChangeTimes->{3.40483095753125*^9}, > CellLabel->"Out[1]="] > > This produces an input dialog box that times out without accepting > input. if I change the default > > Method->"Preemptive" -- to---> Method->"Queued" > > My first question is , "Why must I change the method?". Because Preemptive means that the kernel is interrupted in doing what it does for a quick calculation so that these buttons are reactive even when the kernel is busy doing something else. To make sense these actions are limited to a default of 5 or 6 seconds. Using Queued, the evaluation is done in the usual way like when evaluating cells, that is it is queued, and of course then the need for a timeout does not apply. To learn more about this, read the tutorials for Dynamic. > Having made the method change, I can enter input and without the If[ ] > write the input to the selected notebook. > But the conditional does not work. If I enter input and click OK, > nothing is entered into the selected notebook. > > The purpose of the conditional If[ ] is to avoid the appearance of > "$Canceled" in the selected notebook when one clicks on "Cancel" or "X" > in the input dialog box. > > My second question has two parts: 1) "Why doesn't the If[ ] work; 2) > what will work to accomplish my purpose?". This is a very common pitfall when using == which is in mathematica used for mathematical equivalence and usualy is returned unevaluated when either the right or lefthand side contain a symbol, which you can check by evaluating this: "input string" == $Canceled Usually if you are not handling mathematical expression SameQ (===) is a better choice, like: "input string" === $Canceled which will be evaluated to False. So the following should cure your problems: Button[ Style["LABEL", Black, 10, FontFamily -> "Arial Black"], tag=InputString[]; nb=SelectedNotebook[]; If[tag === $Canceled, NotebookWrite[nb,Cell[tag,"Text"]]], Method->"Queued" ] hth, albert