Hot answers tagged

22

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

I'm not sure if this is exactly what you need but this is what I've recently done to inform the user about ongoing calculation. Usage withProgressIndicator[proc, delay] Performs a proc, and when it lasts longer than delay (default 0), a progress indicator in dialog is created. It will be closed after finishing the proc. It should be run on Main Link, ...


16

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 ->...


15

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 ...


13

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


13

Building on Kubas code here is an alternative which solves some (minor) problems: it will not leave behind scheduled tasks when the abort button is used, it works equaly well when the Alt+. shortcut is used to abort and it might work better when aborting preemptive evaluations. Here is the code: SetAttributes[withProgressIndicator, HoldFirst]; ...


13

To understand this, look at the typesetting: In[1]:= ToBoxes[Button["Print", Dynamic[a++]]] // InputForm Out[1]//InputForm= ButtonBox["\"Print\"", ButtonFunction :> Dynamic[a++], Appearance -> Automatic, Evaluator -> Automatic, Method -> "Preemptive"] Front end options, which includes all box options, can take Dynamic heads. That basically ...


13

Why not just use the graphic in the question, or any other images that you like: Use ImageSize to control the final size of the button: An alternative, that I use in some applications, is something like this: help[$helpmessage_: "", $videolink_: ""] := ActionMenu[ Graphics[{{RGBColor[0.689647, 0.761166, 0.805478], Disk[{0, 0}, 0.1]}, {...


12

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@##]], #...


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 to ...


11

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.


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. That indicates: the front end sends one evaluation at a time and waits for the result before continuing with its other work Tutorial / Advanced Dynamic Functionality / ...


11

So I mentioned this in a comment but I'll get it out here for the bounty poster too. Searching for the "JLink`" in the autocomplete directory gave me this: FileNameJoin@{$InstallationDirectory, "SystemFiles", "Components", "AutoCompletionData", "Main", "documentedContexts.m"} If we look at what's inside it: FileNameJoin@{$InstallationDirectory, "...


10

A couple of images that come with Mathematica. Caveat: There's no guarantee that they will be present in all (future) versions. Button[ Import[FileNameJoin[{$InstallationDirectory, "SystemFiles", "FrontEnd", "SystemResources", "Bitmaps", "Popups", "CodeCompletion", "MenuItemHelpTiny@144dpi.png"}] ], Beep[], (* your help dialog here *) ...


10

Here's the general way this type of thing seems to be handled in the built-in things plus code to build the type of buttons you want. First the button code: makeDisk[gradSpec_, n_] := Rasterize[ Module[{img, disk, edge}, img = RadialGradientImage[ gradSpec, n ]; Graphics[ { Inset[img, Center, Center, Scaled[1]]...


10

ClearAll[togglerMesh] togglerMesh = DynamicModule[{ m = #, ids = {}, nF = Nearest[PropertyValue[{#, 2}, MeshCellCentroid] -> "Index"]}, Dynamic@EventHandler[HighlightMesh[m, Thread[{2, Flatten@ids}]], "MouseClicked" :> If[MemberQ[ids, #], ids = DeleteCases[ids, #], AppendTo[ids, #]] &[If[MousePosition["Graphics"] === ...


9

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 "...


9

Here are implementations for a MeshTogglerBar and MeshSetterBar based on my answer here (code below). Both implementations use Mouseover and EventHandler to handle detection of the polygon below the cursor for you. Compared to the NearestFunction approach, this is far more performant (since it is done by the front-end), it also works nicely for other types ...


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}] // ...


8

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


8

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 -> Dynamic@If[CurrentValue["...


8

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 = (...


8

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 ...


8

SeedRandom[421] points = RandomReal[{-1, 1}, {10, 2}]; mesh = VoronoiMesh[points, {{-1, 1}, {-1, 1}}]; nf = Nearest[points -> Automatic]; primitives = MeshPrimitives[mesh, {2, All}]; Dynamic[ ClickPane[ HighlightMesh[mesh, {2, #}] & @@ FirstPosition[ primitives, SelectFirst[primitives, RegionMember[#, Extract[points, ...


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 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]


7

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


7

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 *) ...


7

Another method which doesn't really use Mouseover but the EventHandler: DynamicModule[{col = Red}, EventHandler[ Button[ Panel["Print", FrameMargins -> {{4, 4}, {4, 4}}, Background -> Dynamic[col]], ...


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}]]]


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