Hot answers tagged

19

If you pass SynchronousUpdating->False to Dynamic, it will perform operations on the main link. Note that this only works where Dynamic is displayed as a typeset result (i.e., typeset as a DynamicBox). It does not presently work where Dynamic is used to give a value to a control (such as Slider) or an option. A quick survey of other constructs... ...


16

The raster method I alluded to in a comment was requested. g1 = Graphics[{ Polygon[{{0, 0}, {3, 0}, {3, 1}, {0, 1}}, VertexColors -> {Red, Red, Blue, Blue}] }] g2 = Graphics[{Rectangle[{0, 0}, {3, 1}, RoundingRadius -> 0.5]}] ImageAdd[g1, g2]


12

Lets get an image: img = ExampleData[{"TestImage", "Lena"}]; Overlay is pretty easy to use for location specification. In the case below I used scaled coordinates: Overlay[{img, Button[Style["Image Histogram", Blue, Italic, 34], CreateDialog[ImageHistogram[img]]]}, All, 2, Alignment -> {.7, -.8}] Here is a simple line to understand better how ...


12

Look at CompoundExpression : Button["Click Here", Print[10!]; Print[11!]] "Click Here" when clicked it performs two actions 3628800 39916800


12

While John Fultz gave a depressing answer concerning GUI controls, I doubted that this cannot be done in Mathematica. A bit of exploration and Rojo's extremely useful answer helped me to come up with a workaround to simulate Method -> Queued for GUI controllers other than Button. The function queued accepts any dynamic controller as its first argument ...


11

Edit One can use either an image-based (hence rasterized) or a vector-based (resolution-independent) approach to get the rounded corners. I'll first discuss the vector based solution, and then add a raster-based solution. Although Mr. Wizard already posted a raster-based approach, I think it can be improved. Update The function roundedGraphics is ...


11

It looks like you can use VertexShapeFunction to do it (also take a look at the other options for Graph). Modifying one of the examples from the documentation: Graph[{1 -> 2, 2 -> "bob", "bob" -> 1}, VertexShapeFunction -> (Inset[ Tooltip[ Button[#2, Speak["vertex " <> ToString[#2]]], Column[{"arguments:"}~Join~List@##]], ...


11

The reason is because Button actions are calculated on a preemptive link, meaning they preempt any other evaluation, but are only allowed a certain amount of time to evaluate. You can replicate the behavior of Print@1; Pause@1; Print@2; by adding the option Method->"Queued" to the Button arguments. This ensures the actions are performed in the ...


10

This answer uses RegionPlot to plot the rounded rectangle. In roundedRect, {{xmin, xmax}, {ymin, ymax}} is the range of the rectangle and rad the rounding radius. roundedRect accepts any option of RegionPlot, in particular ColorFunction which you can use to shade the rectangle. Options[roundedRect] = Options[RegionPlot]; SetOptions[roundedRect, {Frame ...


10

Vitaliy had a great answer. I guess another way to do this is to simply make the curves using many lines: In the following code, resolution is the number of lines used to make the curve and m is how big the corners are. resolution = 30; w = 2; h = 1; m = 0.1; circlePoint[center_, radius_, radian_] := radius {Cos[radian], Sin[radian]} + ...


9

This behaviour is explained in the documentation of Button under Examples > Options > Method. By default, button functions are evaluated on a preemptive link which times out after 5 seconds. To prevent the code from timing out you can set Method -> "Queued" which will run the code on the main link.


8

You could do Table[ With[{i = i}, Button["Number: " <> ToString@i, Print@i]], {i, 1, 5}] The reason is that Attributes@Button (*{HoldRest, Protected, ReadProtected}*) so that the code you produce ends up containing things like Button["Number: 2", Print[i]] (try looking at Table[Button["Number: " <> ToString@i, Print@i],{i, 1, 5}] // ...


7

(from Wolfram tech support) There is no direct way of doing this but the work around is to set the button appearance to "Pressed" and set the button background to the inverse of the background you actually want. So for a white button: Button["xxx", Print@"test", Appearance -> {None, "Pressed"}, Background -> Black] This gives you a button that does ...


7

You could try something like: y = 0; b1 = Button["Evaluate", y = Cos[Pi/6] (++y), Method -> "Queued"] Dynamic@y


7

Based on Istvans solution this should do the same thing, but is somewhat simpler in that it avoids the EventHandler which would need adoption to match the possible interactions of the gui element used. The use of the three "change functions" also makes possible to continuously update the controller variable but only trigger the long calculation when the ...


7

If you want full flexibility you should try EventHandler and graphics primitives. If you can decipher the following example you will get the idea: color = Black; positions = Position[DiskMatrix[5], 1]; disks = {color, Disk[#, 0.4]} & /@ positions; eventHandler[item : {c_, obj_: Disk[p_, _]}] := {c, EventHandler[obj, "MouseClicked" :> (disks = ...


7

One way, based on the self-delete example in the documentation for Button: Button["Start", SelectionMove[ButtonNotebook[], All, GeneratedCell]; NotebookDelete[ButtonNotebook[]]; Print[Plot[x^3, {x, -1, 1}]]]


7

The code above 9.0 is good Unfortunately, this is not true. Even in version 9 this code was not good, but it just did not warn you about it. Take this simpler example, that only prints the file-name of the selected file and try it in version 9: Button["Import File", Print[SystemDialogInput["FileOpen"]]] If you try the button and select a file ...


7

Another way is to use Grid for complex layouts. One drawback is that Delimiter does not work as in the original Manipulate, the advantage is that you can use all styling options available to Grid, plus you donĀ“t have to fiddle with discrete ImageSize values etc.: Manipulate[ ParametricPlot[{y t^3 - u, x t^2 + z}, {t, -10, 10}, PlotRange -> {{-1.5, ...


7

According to the documentation, "PasteButton, evaluates its arguments in an ordinary way, so that expr is immediately evaluated" It is like you evaluate Plot[\[SelectionPlaceholder], {x, 0, 10}] which will give empty plot. This could be one solution: PasteButton["Plot", Defer[Plot[\[SelectionPlaceholder], {x, 0, 10}]]]


7

In case you want to plot in place you can use something like: Button["Plot", NotebookWrite[ InputNotebook[], ToBoxes @ Plot[ Evaluate @ ToExpression @ CurrentValue @ "SelectionData", {x, 0, 10} ] ] ] You may want to add Method -> "Queued" for more complicated plot to avoid timeout. And if you want x from ...


6

Use CompoundExpression, which will be more familiar in the form of the ; operator (which many wouldn't recognize as an operator at all). This works anywhere where a single command seems to be called for in a syntax description. CompoundExpression returns the result of the last operation or Null if there isn't any.


6

Use ColorFunction along a single dimension for gradient and a smart analytic curve for boundary. You can easily control type of color gradient via ColorFunction. RegionPlot[.7 x^8 + 80 y^8 < .3, {x, -2, 2}, {y, -2, 2}, Frame -> False, Axes -> False, ColorFunction -> Function[{x, y}, Hue[.3 y]]]


6

You can use an inset: Graphics[{Inset[ RandomImage[CauchyDistribution[0, .2], {100, 100}, ColorSpace -> "RGB"], Center], Inset[Button["Click Here", Print[10!]], {Center, -0.5}]}, Frame -> True]


6

Of course! button := Button["Press me!", list = Drop[list, 1]]; list = Table[button, {5}]; Dynamic[list]


6

You don't have repeat yourself. You can map a pure function defining the button over a list of the background colors and then apply Mouseover Like so: Mouseover @@ (Button[Panel["Print", FrameMargins -> {{4, 4}, {4, 4}}, Background -> #], Print["Print"], Appearance -> None] & /@ {Red, Green})


6

For simplicity, I'm using function downvalues as a backend (and not an array) (* Define functions *) bTable[action_, fArray_, dims_]:= Grid@Array[Button[{#1, #2}, action[fArray, #1, #2]]&, dims] action[fArray_, x_, y_] := (fArray[x, y] = fArray[x, y] + 1) (* or whatever *) (* Initialize *) dims = {3, 3}; Array[(f[#1, #2] = 0) &, dims]; (* Run *) ...


6

An alternative approach using CurrentValue["MouseOver"]: Button[Panel["Print", FrameMargins -> {{4, 4}, {4, 4}}, Background -> Dynamic@If[CurrentValue["MouseOver"], Green, Red]], Print["Print"], Appearance -> None] or, without the Panel, Button["Print", Print["Print"], Background -> ...


6

OK, I guess I found something myself while trying to circumvent RunScheduledTask. DynamicModule[{prog = False}, Column[{ Button[ "Do heavy work", prog = True; Pause[10]; prog = False, Method -> "Queued" ], Dynamic@If[prog, ProgressIndicator[Appearance -> "Percolate"], Invisible[ProgressIndicator[Appearance ...


6

Add Method -> "Queued" Button["Import File", Import[SystemDialogInput["FileOpen", ".txt"], "List"], Method -> "Queued"] From IntroductionToControlObjects So if you have an evaluation that you expect will take more than a couple of seconds to evaluate, you should use Method->"Queued". Clicking such a button will use the main link when ...



Only top voted, non community-wiki answers of a minimum length are eligible