Skip to main content

dynamic - Why do buttons with ChoiceDialog freeze the front end?


Pressing the button below will freeze my front end. An unpainted white dialog box comes up, then it completely freezes, and I need to forcibly kill the front end. CPU usage stays at zero during this.


Button["Ask", If[ChoiceDialog["OK?"], Print["OK!"]]]


Why does this happen?


What is the proper way to ask for confirmation before an action?




Answers:


My first question has been answered below and in the comment by @JohnFultz: use Button[..., Method -> "Queued"]. But this will make a button which doesn't work while evaluations are ongoing.


I need a button which works even during evaluations. I came up with the following solution:


Button["Ask", 
CreateDialog[
Column[{"OK?",

ChoiceButtons[{"OK"}, {Print["OK!"]; DialogReturn[]}]}]]]

CreateDialog returns immediately without waiting for an input, so the front end is not blocked. The functionality is moved completely into the dialog's button.


You can test that the button works and reacts immediately even while Pause[10] is evaluating.




Quoting the two most enlightening comments:



The "Preemptive" method interrupts queued (e.g., Shift+Enter) evaluations to do its work and cannot itself be interrupted. I.e., you can't preempt a preemptive calculation. ChoiceDialog is putting up an interface which independently needs the attention of the Mathematica kernel, but can't get it because Button has the kernel's sole attention - John Fultz






Button[..., Method->"Preemptive"] (the default Method value) and InputString[] are effectively exactly the opposite of each other. Button[..., Method->"Preemptive"] will pause the user interface (causing it to stop responding to events) until the computation is complete. InputString[] will pause the computation until the user has input some value. When you combine these two the user interface is waiting for the computation to complete while at the same time the computation is waiting for user interface input. - ragfield




Answer



Try the following two alternatives, pressing the button while the Do loop is waiting.


Button["Click Here", Print[10!], Method -> "Preemptive"]
Do[Print[x]; Pause[5], {2}]
(*
-> x
-> 3628800

-> x
*)

and


Button["Click Here", Print[10!], Method -> "Queued"]
Do[Print[x]; Pause[5], {2}]
(*
-> x
-> x
-> 3628800

*)

Some Explanation:


You can see that in the first case, the Do loop is interrupted for processing the Button procedure, but in the second case it is just queued and the Button processing is postponed until the Do loop ends.


In the case of the "Preemptive" method, Mma wants to assure that you are not stealing the CPU forever and impeding the Do loop finalization, so it gives you a certain time slice to process the Button procedure. If you yield control to human input (as in InputString[]), that time slice expires and thus the results you are getting.


Comments

Popular posts from this blog

mathematical optimization - Minimizing using indices, error: Part::pkspec1: The expression cannot be used as a part specification

I want to use Minimize where the variables to minimize are indices pointing into an array. Here a MWE that hopefully shows what my problem is. vars = u@# & /@ Range[3]; cons = Flatten@ { Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; Minimize[{Total@((vec1[[#]] - vec2[[u[#]]])^2 & /@ Range[1, 3]), cons}, vars, Integers] The error I get: Part::pkspec1: The expression u[1] cannot be used as a part specification. >> Answer Ok, it seems that one can get around Mathematica trying to evaluate vec2[[u[1]]] too early by using the function Indexed[vec2,u[1]] . The working MWE would then look like the following: vars = u@# & /@ Range[3]; cons = Flatten@{ Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; NMinimize[ {Total@((vec1[[#]] - Indexed[vec2, u[#]])^2 & /@ R...

functions - Get leading series expansion term?

Given a function f[x] , I would like to have a function leadingSeries that returns just the leading term in the series around x=0 . For example: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x)] x and leadingSeries[(1/x + 2 + (1 - 1/x^3)/4)/(4 + x)] -(1/(16 x^3)) Is there such a function in Mathematica? Or maybe one can implement it efficiently? EDIT I finally went with the following implementation, based on Carl Woll 's answer: lds[ex_,x_]:=( (ex/.x->(x+O[x]^2))/.SeriesData[U_,Z_,L_List,Mi_,Ma_,De_]:>SeriesData[U,Z,{L[[1]]},Mi,Mi+1,De]//Quiet//Normal) The advantage is, that this one also properly works with functions whose leading term is a constant: lds[Exp[x],x] 1 Answer Update 1 Updated to eliminate SeriesData and to not return additional terms Perhaps you could use: leadingSeries[expr_, x_] := Normal[expr /. x->(x+O[x]^2) /. a_List :> Take[a, 1]] Then for your examples: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x), x] leadingSeries[Exp[x], x] leadingSeries[(1/x + 2 + (1 - 1/x...

plotting - Plot 4D data with color as 4th dimension

I have a list of 4D data (x position, y position, amplitude, wavelength). I want to plot x, y, and amplitude on a 3D plot and have the color of the points correspond to the wavelength. I have seen many examples using functions to define color but my wavelength cannot be expressed by an analytic function. Is there a simple way to do this? Answer Here a another possible way to visualize 4D data: data = Flatten[Table[{x, y, x^2 + y^2, Sin[x - y]}, {x, -Pi, Pi,Pi/10}, {y,-Pi,Pi, Pi/10}], 1]; You can use the function Point along with VertexColors . Now the points are places using the first three elements and the color is determined by the fourth. In this case I used Hue, but you can use whatever you prefer. Graphics3D[ Point[data[[All, 1 ;; 3]], VertexColors -> Hue /@ data[[All, 4]]], Axes -> True, BoxRatios -> {1, 1, 1/GoldenRatio}]