Tag Info

Hot answers tagged

18

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


14

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


11

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


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


10

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


10

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


10

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


8

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

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


8

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


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


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

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


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

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


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


5

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


5

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]


5

Since your parameter i in Button is outside Table (because of the HoldRest attribute of Button), it is not a number anymore. However you could do e.g. this : Button["Number: " <> ToString @ #, Print @ #] & /@ Range[5] If there is a need for i parameter one can do this : Button["Number: " <> ToString @ #, Print @ # ] & /@ Table[i, {i, ...


5

If you only want to remove one button, the solution is easy: list = {Button[1], Button[2, list = Delete[list, 2]], Button[3]}; Dynamic@list If you want to remove multiple unique buttons, you have to use some kind of identification for each button other than the actual position in the list, as that is changed when one of them is removed. Here I use a ...


5

Check what The Futz just said. However, you can go the ugly workaround way. Not recommended, since I don't think you have any guarantees that your code will be evaluated exactly once only when you click the button, but up to you. Try this Print@"I dare you to move the slider after pressing the button"; \ Slider[] EventHandler[Framed["Benjamin Button"], ...


5

Done! Table[DynamicModule[{x = 0}, Button[i + 10 j, x = Mod[x + .5, 1], Background -> Dynamic[Hue[x]]]], {j, 0, 9}, {i, 1, 10}] // Grid


4

You can do : Table[With[{i = i}, Button["Number: " <> ToString@i, Print[i]]], {i, 1, 5}]


4

Try using Button["Recalculate", codeset, Method->"Queued"] Shift+Enter starts a queued evaluation, while the button, by default, a preemtive one, which blocks the front end and has a timeout


4

One can come up with some messy method to evaluate a front-end button programmatically, but I think it is easier to separate the button functionality from the button itself, as listenerButtonFunction[]. Here I introduce a timer button that, if pushed, counts down simulating a long calculation. When it finishes, it switches the flag active to True. The second ...


4

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


4

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


4

Explanation in detail what this code does: Since the Button[..., Background -> color] has a rather ugly look, I use a Panel with colored background as the label of the button, this causes the button-in-a-button look. When simply clicked (pressed and released immediately), nothing special happes, the button (and Panel) only changes appearance for the ...


4

It's hard to tell exactly what the problem is, but here is one way: f[n_] := Print[Range[n]]; Button["name", f[7]] Now when you press the button, it prints from 1 to 7 (or whatever number you place in the f[ ]. The action of the function f is invoked whenever you press the button, so it could set values, print, graph things, define variables, and ...



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