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

front end - keyboard shortcut to invoke Insert new matrix

I frequently need to type in some matrices, and the menu command Insert > Table/Matrix > New... allows matrices with lines drawn between columns and rows, which is very helpful. I would like to make a keyboard shortcut for it, but cannot find the relevant frontend token command (4209405) for it. Since the FullForm[] and InputForm[] of matrices with lines drawn between rows and columns is the same as those without lines, it's hard to do this via 3rd party system-wide text expanders (e.g. autohotkey or atext on mac). How does one assign a keyboard shortcut for the menu item Insert > Table/Matrix > New... , preferably using only mathematica? Thanks! Answer In the MenuSetup.tr (for linux located in the $InstallationDirectory/SystemFiles/FrontEnd/TextResources/X/ directory), I changed the line MenuItem["&New...", "CreateGridBoxDialog"] to read MenuItem["&New...", "CreateGridBoxDialog", MenuKey["m", Modifiers-...

How to thread a list

I have data in format data = {{a1, a2}, {b1, b2}, {c1, c2}, {d1, d2}} Tableform: I want to thread it to : tdata = {{{a1, b1}, {a2, b2}}, {{a1, c1}, {a2, c2}}, {{a1, d1}, {a2, d2}}} Tableform: And I would like to do better then pseudofunction[n_] := Transpose[{data2[[1]], data2[[n]]}]; SetAttributes[pseudofunction, Listable]; Range[2, 4] // pseudofunction Here is my benchmark data, where data3 is normal sample of real data. data3 = Drop[ExcelWorkBook[[Column1 ;; Column4]], None, 1]; data2 = {a #, b #, c #, d #} & /@ Range[1, 10^5]; data = RandomReal[{0, 1}, {10^6, 4}]; Here is my benchmark code kptnw[list_] := Transpose[{Table[First@#, {Length@# - 1}], Rest@#}, {3, 1, 2}] &@list kptnw2[list_] := Transpose[{ConstantArray[First@#, Length@# - 1], Rest@#}, {3, 1, 2}] &@list OleksandrR[list_] := Flatten[Outer[List, List@First[list], Rest[list], 1], {{2}, {1, 4}}] paradox2[list_] := Partition[Riffle[list[[1]], #], 2] & /@ Drop[list, 1] RM[list_] := FoldList[Transpose[{First@li...

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