Calculator in Go and Fyne
Calculator in Go and Fyne
Now to cross-check if our created app works fine, just click on the “Hi!”
symbol and see if the text “Hello Fyne !” changes to “Welcome :)”
! Now let us build our own calculator!
Our first step after creating the particular window from the above
snippet would be to create buttons for our calculator.
Now it's your task to create all the buttons using similar syntax.
Now it's time to set our content inside our window, in short, we
need to show our buttons on our window.
Note: Whatever we write in our window is always treated as a row.
We’ll treat our buttons as one box, so now all of our buttons are part of
one box, inside that box, we’ll create another box that will contain
columns according to the buttons available in that particular row.
I.e.
Body of box -> Another Box containing two columns (History and Clear
button) -> another box containing 4 columns -> below that another box
containing 4 columns -> below that another box containing next 4
columns -> then our second last box containing 4 columns -> then our
last box containing 3 columns.
The layout we are talking about looks like this:
To create the boxes our syntax of code will look like this:
Similarly, we’ll create all the columns with the help of our similar syntax-
Change the name of your window accordingly.
Now we’ll take a variable named “output” and we’ll pass an empty string
in it. After that, we’ll take an “input” variable for our new label of the
widget and will pass the output in it.
After this, let’s figure out what will happen when we press the buttons,
i.e. let us move forward and see the functionality of our buttons:
Except “History”, “Clear” and “Equals to” buttons, all the functions will
have a similar type of syntax:
For example, whenever we click on our clear button, we want our output
to be an empty string. For that, we’ll keep out output as an empty string
and then set the output using.SetText inside the input variable.
Similarly, we’ll make changes for the rest of the buttons:
We’ll only perform back operation when the length of our substring is
greater than zero. We’ll set out the output from “0 to length” and then
subtract 1 from it to reduce the substring.
In the end, well set out output text in our input variable.
Now we’ll pass our output string inside our evaluable expression, Now if
my expression is evaluable then that expression should be my output,
otherwise, if it’s not evaluable then my output should print “Error”.
If I don’t have any error then I want my output to be equal to the “result”
value. The question that arises is-
“How do I convert the result value into a string?”
For that we have s special expression for you, add this expression
instead of the result and import the “strconv” inside our import function.
Which look as follows:
Now our final task is to write functionality for the “History Button”
Our first step would be creating an array then we’ll keep appending the
output in it.
When will “History” get created? Only after we perform equal to “=”
operation, right?
So let us make a few changes inside our equal to function. Firstly we’ll
store out output string value inside a new variable, let’s say ans and
then we’ll append the output expression with answer into our string.
Now we’ll append our strToAppend string inside our History array and
then we will set our output to our answer.
Now inside our history function, we’ll run a reverse for loop because we
want to print the last string of operation as our recent most history.
Then well add the history array inside our history string.
And will print each operation on a new line.
After that, we’ll create a variable names “isHistory” and will initialize it to
a false value.
We’ll put a condition, which will empty the history string if clicked twice.
Outside our conditions, we’ll toggle the value of isHistory and then set
our history string.
In the end, our History function will look as follows:
Now if you want to resize your calculator prompt then use the following
command or the same, and adjust the final layout as per your own
convenience.
You can see that we have a different size dimension for equal to
button(“=”), for doing that we can use the following syntax:
package main
import (
"strconv"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
"github.com/Knetic/govaluate"
)
func main() {
a := app.New()
w := a.NewWindow("Calculator !")
w.Resize(fyne.NewSize(300,280))
output:= ""
input:= widget.NewLabel(output)
isHistory:=false;
historyStr:=""
history:=widget.NewLabel(historyStr)
var historyArr[]string;
historyBtn:=widget.NewButton("History", func() {
if isHistory{
historyStr = ""
}else{
for i:= len(historyArr)-1; i>=0;i--{
historyStr = historyStr+historyArr[i];
historyStr+="\n"
}
}
isHistory = !isHistory
history.SetText(historyStr);
})
backBtn:=widget.NewButton("Back", func() {
if len(output) > 0{
output = output[:len(output)-1];
input.SetText(output);}
})
clearBtn:=widget.NewButton("Clear", func() {
output = "";
input.SetText(output);
})
openBtn:=widget.NewButton("(", func() {
output = output+"(";
input.SetText(output);
})
closeBtn:=widget.NewButton(")", func() {
output = output+")";
input.SetText(output);
})
divideBtn:=widget.NewButton("/", func() {
output = output+"/";
input.SetText(output);
})
sevenBtn:=widget.NewButton("7", func() {
output = output+"7";
input.SetText(output);
})
eightBtn:=widget.NewButton("8", func() {
output = output+"8";
input.SetText(output);
})
nineBtn:=widget.NewButton("9", func() {
output = output+"9";
input.SetText(output);
})
multiplyBtn:=widget.NewButton("*", func() {
output = output+"*";
input.SetText(output);
})
fourBtn:=widget.NewButton("4", func() {
output = output+"4";
input.SetText(output);
})
fiveBtn:=widget.NewButton("5", func() {
output = output+"5";
input.SetText(output);
})
sixBtn:=widget.NewButton("6", func() {
output = output+"6";
input.SetText(output);
})
minusBtn:=widget.NewButton("-", func() {
output = output+"-";
input.SetText(output);
})
oneBtn:=widget.NewButton("1", func() {
output = output+"1";
input.SetText(output);
})
twoBtn:=widget.NewButton("2", func() {
output = output+"2";
input.SetText(output);
})
threeBtn:=widget.NewButton("3", func() {
output = output+"3";
input.SetText(output);
})
plusBtn:=widget.NewButton("+", func() {
output = output+"+";
input.SetText(output);
})
zeroBtn:=widget.NewButton("0", func() {
output = output+"0";
input.SetText(output);
})
dotBtn:=widget.NewButton(".", func() {
output = output+".";
input.SetText(output);
})
equalrBtn:=widget.NewButton("=", func() {
expression, err:=
govaluate.NewEvaluableExpression(output);
if err ==nil{
result, err := expression.Evaluate(nil);
if err==nil{
ans:= strconv.FormatFloat(result.(float64),'f', -1,
64);
strToAppend:=output+ "=" +ans;
})
w.SetContent(container.NewVBox(
input,
history,
container.NewGridWithColumns(1,
container.NewGridWithColumns(2,
historyBtn,
backBtn),
container.NewGridWithColumns(4,
clearBtn,
openBtn,
closeBtn,
divideBtn),
container.NewGridWithColumns(4,
sevenBtn,
eightBtn,
nineBtn,
multiplyBtn),
container.NewGridWithColumns(4,
fourBtn,
fiveBtn,
sixBtn,
minusBtn),
container.NewGridWithColumns(4,
oneBtn,
twoBtn,
threeBtn,
plusBtn),
container.NewGridWithColumns(2,
container.NewGridWithColumns(2,
zeroBtn,
dotBtn,
),
equalrBtn),
),
))
w.ShowAndRun()
}
You can find the same article in lecture form on our youtube channel.
For the YouTube video link of “Calculator in Go and fyne” - click here
!!! We hope you found this article helpful !!!
For more free study resources and information about the courses, visit: