Skip to main content

gui construction - Better alternatives for GUIkit or dialogboxes?


I'm trying to create a custom interface, where I input equivalent electrical resistances which were previously measured, so I can solve a system of 8 non linear equations using FindRoot to get the actual resistances values I want (for two wheatstone bridges). My aim is to have an interface where I can easily input these measurements and obtain the values I want inside boxes, after the click of a button. I started by using GUIkit, but then learned that it was deprecated and went on using CreateDialog, and got this until now:


CreateDialog[
Column[{Row[{TextCell["Equivalent Resistance 1: "],
InputField[Dynamic[req1], Number, FieldSize -> {5, 1}]}],
Row[{TextCell["Equivalent Resistance 2: "],
InputField[Dynamic[req2], Number, FieldSize -> {5, 1}]}],
Row[{TextCell["Equivalent Resistance 3: "],
InputField[Dynamic[req3], Number, FieldSize -> {5, 1}]}],

Row[{TextCell["Equivalent Resistance 4: "],
InputField[Dynamic[req4], Number, FieldSize -> {5, 1}]}],
Row[{TextCell["Equivalent Resistance A: "],
InputField[Dynamic[reqa], Number, FieldSize -> {5, 1}]}],
Row[{TextCell["Equivalent Resistance B: "],
InputField[Dynamic[reqb], Number, FieldSize -> {5, 1}]}],
Row[{TextCell["Equivalent Resistance C: "],
InputField[Dynamic[reqc], Number, FieldSize -> {5, 1}]}],
Row[{TextCell["Equivalent Resistance D: "],
InputField[Dynamic[reqd], Number, FieldSize -> {5, 1}]}],

Button[
"Compute", {Clear[req1, req2, req3, req4, reqa, reqb, reqc,
reqd]}]
}],
WindowTitle -> "Wheatstone Bridges"
];

It doesn't do anything useful yet, but my question is: Are there any other tools I would be better off with, to do this rather than using DialogBoxes? I feel that using this method is giving me more problems than when I was using GUIkit, which felt quite intuitive. Do you recommend other softwares, like MatLab, to do these kinds of custom interfaces? Thank you for your insight.


UPDATE:


@m_Goldberg, Thank you very much for the time spent helping me! After reading the documentation I came up with the following, functional, program:



CreateDialog[
DynamicModule[{inputs, outputs, req1, req2, req3, req4, reqa, reqb,
reqc, reqd, r1, r2, r3, r4, ra, rb, rc, rd, sol, sol1, sol2, sol3,
sol4, sola, solb, solc, sold, eq1, eq2, eq3, eq4, eqa, eqb, eqc,
eqd, init1 = 1, init2 = 1, init3 = 1, init4 = 1, inita = 1,
initb = 1, initc = 1, initd = 1, unit},
unit = Row["ola"];
inputs =
MapThread[
Row[{"Equivalent MS", #1, ": ",

InputField[Dynamic@#2, Number,
FieldSize -> {5, 1}]}] &, {{"1", "2", "3", "4", "A", "B",
"C", "D"}, {req1, req2, req3, req4, reqa, reqb, reqc, reqd}}];

outputs =
MapThread[
Row[{" MS", #1, ": ",
Dynamic@
Pane[Dynamic@#2, 100, BaseStyle -> {Background -> White},
FrameMargins -> 4]}] &, {{"1", "2", "3", "4", "A", "B", "C",

"D"}, {sol1, sol2, sol3, sol4, sola, solb, solc, sold}}
];

req1 =
req2 = req3 =
req4 = reqa =
reqb = reqc =
reqd = sol1 =
sol2 = sol3 = sol4 = sola = solb = solc = sold = 0;


Column[{Row[{Column[Join[inputs, {}]

],
Column[outputs]

}],
Row[{Button[
"Reset", {req1 =
req2 = req3 =
req4 = reqa =

reqb = reqc =
reqd = sol1 =
sol2 = sol3 = sol4 = sola = solb = solc = sold = 0,
init1 = 1, init2 = 1, init3 = 1, init4 = 1, inita = 1,
initb = 1, initc = 1, initd = 1}],

Button["Compute",
If[
req1 == 0 || req2 == 0 || req3 == 0 || req4 == 0 ||
reqa == 0 || reqb == 0 || reqc == 0 ||

reqd == 0, {sol1 =
sol2 = sol3 = sol4 = sola = solb = solc = sold = 0}, {


eq1 = (r1*(((r3 +
r4)*(((ra + rb)*(rc + rd))/(ra + rb + rc +
rd)))/(r3 +
r4 + (((ra + rb)*(rc + rd))/(ra + rb + rc +
rd))) +
r2))/(r1 + (((r3 +

r4)*(((ra + rb)*(rc + rd))/(ra + rb + rc +
rd)))/(r3 +
r4 + ((ra + rb)*(rc + rd))/(ra + rb + rc + rd))) +
r2) == req1;

eq2 = (r2*(((r3 +
r4)*(((ra + rb)*(rc + rd))/(ra + rb + rc +
rd)))/(r3 +
r4 + (((ra + rb)*(rc + rd))/(ra + rb + rc +
rd))) +

r1))/(r2 + (((r3 +
r4)*(((ra + rb)*(rc + rd))/(ra + rb + rc +
rd)))/(r3 +
r4 + ((ra + rb)*(rc + rd))/(ra + rb + rc + rd))) +
r1) == req2;

eq3 = (r3*(((r1 +
r2)*(((ra + rb)*(rc + rd))/(ra + rb + rc +
rd)))/(r1 +
r2 + (((ra + rb)*(rc + rd))/(ra + rb + rc +

rd))) +
r4))/(r3 + (((r1 +
r2)*(((ra + rb)*(rc + rd))/(ra + rb + rc +
rd)))/(r1 +
r2 + ((ra + rb)*(rc + rd))/(ra + rb + rc + rd))) +
r4) == req3;

eq4 = (r4*(((r1 +
r2)*(((ra + rb)*(rc + rd))/(ra + rb + rc +
rd)))/(r1 +

r2 + (((ra + rb)*(rc + rd))/(ra + rb + rc +
rd))) +
r3))/(r4 + (((r1 +
r2)*(((ra + rb)*(rc + rd))/(ra + rb + rc +
rd)))/(r1 +
r2 + ((ra + rb)*(rc + rd))/(ra + rb + rc + rd))) +
r3) == req4;

eqa = (ra*(((rc +
rd)*(((r1 + r2)*(r3 + r4))/(r1 + r2 + r3 +

r4)))/(rc +
rd + (((r1 + r2)*(r3 + r4))/(r1 + r2 + r3 +
r4))) +
rb))/(ra + (((rc +


rd)*(((r1 + r2)*(r3 + r4))/(ra + rb + rc +
rd)))/(rc +
rd + ((r1 + r2)*(r3 + r4))/(r1 + r2 + r3 + r4))) +
rb) == reqa;


eqb = (rb*(((rc +
rd)*(((r1 + r2)*(r3 + r4))/(r1 + r2 + r3 +
r4)))/(rc +
rd + (((r1 + r2)*(r3 + r4))/(r1 + r2 + r3 +
r4))) +
ra))/(rb + (((rc +
rd)*(((r1 + r2)*(r3 + r4))/(ra + rb + rc +
rd)))/(rc +
rd + ((r1 + r2)*(r3 + r4))/(r1 + r2 + r3 + r4))) +

ra) == reqb;

eqc = (rc*(((ra +
rb)*(((r1 + r2)*(r3 + r4))/(r1 + r2 + r3 +
r4)))/(ra +
rb + (((r1 + r2)*(r3 + r4))/(r1 + r2 + r3 +
r4))) +
rd))/(rc + (((ra +
rb)*(((r1 + r2)*(r3 + r4))/(ra + rb + rc +
rd)))/(ra +

rb + ((r1 + r2)*(r3 + r4))/(r1 + r2 + r3 + r4))) +
rd) == reqc;

eqd = (rd*(((ra +
rb)*(((r1 + r2)*(r3 + r4))/(r1 + r2 + r3 +
r4)))/(ra +
rb + (((r1 + r2)*(r3 + r4))/(r1 + r2 + r3 +
r4))) +
rc))/(rd + (((ra +
rb)*(((r1 + r2)*(r3 + r4))/(ra + rb + rc +

rd)))/(ra +
rb + ((r1 + r2)*(r3 + r4))/(r1 + r2 + r3 + r4))) +
rc) == reqd;


sol = FindRoot[{eq1, eq2, eq3, eq4, eqa, eqb, eqc,
eqd}, {{r1, init1}, {r2, init2}, {r3, init3}, {r4,
init4}, {ra, inita}, {rb, initb}, {rc, initc}, {rd,
initd}}],
sol1 = sol[[1, 2]], sol2 = sol[[2, 2]], sol3 = sol[[3, 2]],

sol4 = sol[[4, 2]], sola = sol[[5, 2]], solb = sol[[6, 2]],
solc = sol[[7, 2]], sold = sol[[8, 2]]



}]],


Button["Init",
CreateDialog[{TextCell[

"Choose starting points (default = 1):"],
Column[{

Row[{"Init1: ",
InputField[Dynamic[init1], Number, FieldSize -> {5, 1}],
"Init2: ",
InputField[Dynamic[init2], Number, FieldSize -> {5, 1}],
"Init3: ",
InputField[Dynamic[init3], Number, FieldSize -> {5, 1}],
"Init4: ",

InputField[Dynamic[init4], Number,
FieldSize -> {5, 1}]}],

Row[{"InitA: ",
InputField[Dynamic[inita], Number, FieldSize -> {5, 1}],
"InitB: ",
InputField[Dynamic[initb], Number, FieldSize -> {5, 1}],
"InitC: ",
InputField[Dynamic[initc], Number, FieldSize -> {5, 1}],
"InitD: ",

InputField[Dynamic[initd], Number,
FieldSize -> {5, 1}]}],
Row[{DefaultButton[]}]}

]}];],
CancelButton[],
DefaultButton[DialogReturn[]]}]

}]
],

WindowTitle -> "Wheatstone Bridges (k\[CapitalOmega])"
];

It does what I initially explained in the introduction of this question. I'll read your new update to further improve the code. Once again, thank you very , very much!



Answer



I can understand you might be more comfortable with GUIKit if you have done GUI development with other GUI frameworks. But in Version 6, the Mathematica front-end became a programable GUI with the introduction of the wrapper function Dynamic. This made GUI development possible for users with no previous experience with Java and no desire to learn it. It also allowed the full power of Mathematica to be applied to GUI objects.


If you have experience with conventional GUI development, you face not only a fairly steep learning curve, but also an unlearning curve -- your previous experience getting in your way of becoming comfortable with Wolfram Language's way of doing things.


An example of what I mean by applying the "full power" can be seen in this rewrite of your code. I have added quite a bit of functionality, but still reduced the size of the code.


rtn = 42; Dynamic @ rtn


CreateDialog[
DynamicModule[{inputs, req1, req2, req3, req4, reqa, reqb, reqc, reqd, sum},
inputs =
MapThread[
Row[
{"Equivalent Resistance ", #1, ": ",
InputField[Dynamic @ #2, Number, FieldSize -> {5, 1}]}] &,
{{"1", "2", "3", "4", "A", "B", "C", "D"},
{req1, req2, req3, req4, reqa, reqb, reqc, reqd}}];
sum = req1 = req2 = req3 = req4 = reqa = reqb = reqc = reqd = 0;

Column[
Join[
inputs,
{Row[
{"Summation ",
Dynamic @ Pane[sum, 100,
BaseStyle -> {Background -> White},
FrameMargins -> 4]}],
Row[
{Button["Reset",

sum = req1 = req2 = req3 = req4 = reqa = reqb = reqc = reqd = 0],
Button["Compute",
sum = Plus[req1, req2, req3, req4, reqa, reqb, reqc, reqd]],
CancelButton[],
DefaultButton[DialogReturn[rtn = sum]]}]}]]],
WindowTitle -> "Wheatstone Bridges"];

This gives a dialog window that looks like


dialog


It has full dialog functionality.




  • You can carry out a simple computation with Compute button.

  • You can reset all variables for new computation with the Reset button.

  • You can exit the dialog with no side effects with the Cancel button.

  • You can exit the dialog and update the global dynamic variable rtn with the OK button.


This could be all the GUI you need for your purposes -- you might be able to get away with just changing the actions of the Compute and Reset buttons and the label of the Summation pane. But even if that's not true, This code should move you a fair way forward.


Update


It was late at night when I wrote the above and I was tired. After a good sleep and with a refreshed mind, I had second thoughts. They weren't about the functionality of the code, but its style. I think you will find what I show below to better organized and more readable and, therefore, more easily extended and maintained.


CreateDialog[

DynamicModule[
{req1, req2, req3, req4, reqa, reqb, reqc, reqd,
reset, inputs, sum, doReset, doCompute},
Dynamic @
Column[
Join[
inputs,
{Row[
{"Summation ",
Pane[sum, 100,

BaseStyle -> {Background -> White},
FrameMargins -> 4]}],
Row[
{Button["Reset", doReset[]], Button["Compute", doCompute[]],
CancelButton[], DefaultButton[DialogReturn[rtn = sum]]}]}]],
Initialization :> (
inputs =
MapThread[
Row[
{"Equivalent Resistance ", #1, ": ",

InputField[Dynamic@#2, Number,
FieldSize -> {5, 1}]}] &,
{{"1", "2", "3", "4", "A", "B", "C", "D"},
{req1, req2, req3, req4, reqa, reqb, reqc, reqd}}];
doReset[] :=
(req1 = req2 = req3 = req4 = reqa = reqb = reqc = reqd = sum = 0);
doCompute[] :=
(sum = Plus[req1, req2, req3, req4, reqa, reqb, reqc, reqd]);
doReset[])],
WindowTitle -> "Wheatstone Bridges"];


References


Wolfram Language Documentation Center articles



  • tutorial/IntroductionToDynamic

  • tutorial/IntroductionToManipulate

  • tutorial/AdvancedDynamicFunctionality

  • tutorial/AdvancedManipulateFunctionality


Comments

Popular posts from this blog

plotting - How to draw lines between specified dots on ListPlot?

I would like to create a plot where I have unconnected dots and some connected. So far, I have figured out how to draw the dots. My code is the following: ListPlot[{{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4,13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full] I have thought using ListLinePlot command, but I don't know how to specify to the command to draw only selected lines between the dots. Do have any suggestions/hints on how to do that? Thank you. Answer One possibility would be to use Epilog with Line : ListPlot[ {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4, 13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full, Epilog -> { Line[ ...

equation solving - Invert and fit implicitly defined curve

I need to fit an implicitly defined curve. I thought I could get some data out of Solve , and then using FindFit . Therefore, I would like to find the relation the parametric curve defined by $F(x,y)=0$: Solve[-(1/2) + 1/2 (0.41202 BesselK[0, 0.1 Sqrt[x^2 + y^2]] + (0.101483 x BesselK[1, 0.1 Sqrt[x^2 + y^2]])/Sqrt[x^2 + y^2]) == 0, y] But I can't get an output: Solve was unable to solve the system with inexact coefficients or the system obtained by direct rationalization of inexact numbers present in the system. Since many of the methods used by Solve require exact input, providing Solve with an exact version of the system may help. >> Edit: In particular, I would like to fit the data coming from the curve with the expression of another curve, and not with a function $f(x)$. In particular, since this clearly looks like a cardioid , I would like it to fit to something like it. What other strategies could I try?

dynamic - How can I make a clickable ArrayPlot that returns input?

I would like to create a dynamic ArrayPlot so that the rectangles, when clicked, provide the input. Can I use ArrayPlot for this? Or is there something else I should have to use? Answer ArrayPlot is much more than just a simple array like Grid : it represents a ranged 2D dataset, and its visualization can be finetuned by options like DataReversed and DataRange . These features make it quite complicated to reproduce the same layout and order with Grid . Here I offer AnnotatedArrayPlot which comes in handy when your dataset is more than just a flat 2D array. The dynamic interface allows highlighting individual cells and possibly interacting with them. AnnotatedArrayPlot works the same way as ArrayPlot and accepts the same options plus Enabled , HighlightCoordinates , HighlightStyle and HighlightElementFunction . data = {{Missing["HasSomeMoreData"], GrayLevel[ 1], {RGBColor[0, 1, 1], RGBColor[0, 0, 1], GrayLevel[1]}, RGBColor[0, 1, 0]}, {GrayLevel[0], GrayLevel...