Calculator Demo + Source Code – Fyne GUI Golang tutorial 39

Calculator Demo Source Code – Fyne GUI Golang tutorial 39

 

Source Code 

<div>
<div>package main</div>
&nbsp;
<div>import (</div>
<div>    "fmt"</div>
<div>    "image/color"</div>
<div>    "strconv"</div>
&nbsp;
<div>    "fyne.io/fyne/v2"</div>
<div>    "fyne.io/fyne/v2/app"</div>
<div>    "fyne.io/fyne/v2/canvas"</div>
<div>    "fyne.io/fyne/v2/container"</div>
<div>    "fyne.io/fyne/v2/widget"</div>
<div>)</div>
&nbsp;
<div>func main() {</div>
&nbsp;
<div>    a := app.New()</div>
<div>    w := a.NewWindow("calc")</div>
<div>    w.Resize(fyne.NewSize(400, 400))</div>
&nbsp;
<div>    var val1 int = 0</div>
<div>    var val2 int = 0</div>
<div>    var symbol string = ""</div>
<div>    var total string = ""</div>
<div>    var entryText string = ""</div>
&nbsp;
<div>    entry1 := widget.NewEntry()</div>
<div>    entry1.TextStyle = fyne.TextStyle{Bold: true}</div>
<div>    entry1.SetPlaceHolder("Calculator..")</div>
&nbsp;
<div>    btn1 := widget.NewButton("1", func() {</div>
<div>        total = total + "1"</div>
<div>        entryText = entryText + "1"</div>
<div>        entry1.SetText(fmt.Sprint(entryText))</div>
<div>    })</div>
<div>    btn2 := widget.NewButton("2", func() {</div>
<div>        total = total + "2"</div>
<div>        entryText = entryText + "2"</div>
<div>        entry1.SetText(fmt.Sprint(entryText))</div>
&nbsp;
<div>    })</div>
<div>    btn3 := widget.NewButton("3", func() {</div>
<div>        total = total + "3"</div>
<div>        entryText = entryText + "3"</div>
<div>        entry1.SetText(fmt.Sprint(entryText))</div>
&nbsp;
<div>    })</div>
<div>    btn4 := widget.NewButton("4", func() {</div>
<div>        total = total + "4"</div>
<div>        entryText = entryText + "4"</div>
<div>        entry1.SetText(fmt.Sprint(entryText))</div>
<div>    })</div>
<div>    btn5 := widget.NewButton("5", func() {</div>
<div>        total = total + "5"</div>
<div>        entryText = entryText + "5"</div>
<div>        entry1.SetText(fmt.Sprint(entryText))</div>
<div>    })</div>
<div>    btn6 := widget.NewButton("6", func() {</div>
<div>        total = total + "6"</div>
<div>        entryText = entryText + "6"</div>
<div>        entry1.SetText(fmt.Sprint(entryText))</div>
<div>    })</div>
<div>    btn7 := widget.NewButton("7", func() {</div>
<div>        total = total + "7"</div>
<div>        entryText = entryText + "7"</div>
<div>        entry1.SetText(fmt.Sprint(entryText))</div>
<div>    })</div>
<div>    btn8 := widget.NewButton("8", func() {</div>
<div>        total = total + "8"</div>
<div>        entryText = entryText + "8"</div>
<div>        entry1.SetText(fmt.Sprint(entryText))</div>
<div>    })</div>
<div>    btn9 := widget.NewButton("9", func() {</div>
<div>        total = total + "9"</div>
<div>        entryText = entryText + "9"</div>
<div>        entry1.SetText(fmt.Sprint(entryText))</div>
<div>    })</div>
<div>    btn0 := widget.NewButton("0", func() {</div>
<div>        total = total + "0"</div>
<div>        entryText = entryText + "0"</div>
<div>        entry1.SetText(fmt.Sprint(entryText))</div>
<div>    })</div>
<div>    btnDot := widget.NewButton(".", func() {</div>
&nbsp;
<div>    })</div>
&nbsp;
<div>    result := canvas.NewText("Result", color.Black)</div>
<div>    result.TextSize = 30</div>
<div>    result.Alignment = fyne.TextAlignCenter</div>
<div>    btnClear := widget.NewButton("Clear", func() {</div>
<div>        entry1.SetText("")</div>
<div>        total = ""</div>
<div>        result.Text = ""</div>
<div>        entryText = ""</div>
<div>    })</div>
<div>    // btnClear.Text = "NoClear"</div>
<div>    btnPlus := widget.NewButton("+", func() {</div>
<div>        val1, _ = strconv.Atoi(total)</div>
<div>        fmt.Print(fmt.Sprint(val1))</div>
<div>        fmt.Print(fmt.Sprint(val2))</div>
<div>        symbol = "+"</div>
<div>        fmt.Print(symbol)</div>
&nbsp;
<div>        entryText = entryText + "+"</div>
<div>        entry1.SetText(fmt.Sprint(entryText))</div>
<div>        total = ""</div>
<div>    })</div>
<div>    btnMinus := widget.NewButton("-", func() {</div>
<div>        val1, _ = strconv.Atoi(total)</div>
<div>        fmt.Print(fmt.Sprint(val1))</div>
<div>        fmt.Print(fmt.Sprint(val2))</div>
<div>        symbol = "-"</div>
<div>        fmt.Print(symbol)</div>
&nbsp;
<div>        total = ""</div>
<div>        entryText = entryText + "-"</div>
<div>        entry1.SetText(fmt.Sprint(entryText))</div>
&nbsp;
<div>    })</div>
<div>    btnMultiply := widget.NewButton("x", func() {</div>
<div>        val1, _ = strconv.Atoi(total)</div>
<div>        fmt.Print(fmt.Sprint(val1))</div>
<div>        fmt.Print(fmt.Sprint(val2))</div>
<div>        symbol = "*"</div>
<div>        fmt.Print(symbol)</div>
&nbsp;
<div>        total = ""</div>
&nbsp;
<div>        entryText = entryText + "*"</div>
<div>        entry1.SetText(fmt.Sprint(entryText))</div>
<div>    })</div>
<div>    btnDivide := widget.NewButton("/", func() {</div>
<div>        val1, _ = strconv.Atoi(total)</div>
<div>        fmt.Print(fmt.Sprint(val1))</div>
<div>        fmt.Print(fmt.Sprint(val2))</div>
<div>        symbol = "/"</div>
<div>        fmt.Print(symbol)</div>
&nbsp;
<div>        total = ""</div>
&nbsp;
<div>        entryText = entryText + "/"</div>
<div>        entry1.SetText(fmt.Sprint(entryText))</div>
<div>    })</div>
<div>    btnEqual := widget.NewButton("=", func() {</div>
<div>        val2, _ := strconv.Atoi(total)</div>
<div>        if symbol == "+" {</div>
<div>            myanswer := (val1) + (val2)</div>
<div>            fmt.Println("Sum : ", myanswer)</div>
<div>            result.Text = fmt.Sprint(myanswer)</div>
<div>            result.Refresh()</div>
<div>        } else if symbol == "-" {</div>
<div>            myanswer := (val1) - (val2)</div>
<div>            fmt.Println("Minus : ", myanswer)</div>
<div>            result.Text = fmt.Sprint(myanswer)</div>
<div>            result.Refresh()</div>
<div>        } else if symbol == "*" {</div>
<div>            myanswer := (val1) * (val2)</div>
<div>            fmt.Println("Multiply : ", myanswer)</div>
<div>            result.Text = fmt.Sprint(myanswer)</div>
<div>            result.Refresh()</div>
<div>        } else {</div>
<div>            myanswer := (val1) / (val2)</div>
<div>            fmt.Println("divide : ", myanswer)</div>
<div>            result.Text = fmt.Sprint(myanswer)</div>
<div>            result.Refresh()</div>
<div>        }</div>
<div>        total = ""</div>
<div>    })</div>
&nbsp;
<div>    w.SetContent(</div>
<div>        container.NewVBox(</div>
&nbsp;
<div>            entry1,</div>
<div>            result,</div>
<div>            container.NewGridWithColumns(</div>
<div>                4,</div>
<div>                btn1,</div>
<div>                btn2,</div>
<div>                btn3,</div>
<div>                btn4,</div>
<div>                btn5,</div>
<div>                btn6,</div>
<div>                btn7,</div>
<div>                btn8,</div>
<div>                btn9,</div>
<div>                btn0,</div>
<div>                btnDot,</div>
<div>                btnEqual,</div>
<div>                btnPlus,</div>
<div>                btnMinus,</div>
<div>                btnMultiply,</div>
<div>                btnDivide,</div>
<div>                btnClear,</div>
<div>            ),</div>
<div>        ),</div>
<div>    )</div>
<div>    w.ShowAndRun()</div>
<div>}</div>
</div>
Download Code

Fyne Golang GUI Course

 

Tony BB
 

TonyBB is a Coach , marketer, hypnotist and a founder of RSKVF Production who specializes in providing simple, affordable, and easy to use solutions for Life.

Click Here to Leave a Comment Below 0 comments

Leave a Reply: