Sunday, October 23, 2022
HomeWordPress DevelopmentGolang GUI Calculator - DEV Neighborhood πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Golang GUI Calculator – DEV Neighborhood πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»


On this article you’ll study, how one can construct a calculator from scratch. We’ll give attention to the Go and it’s Fyne bundle which helps to create GUI based mostly utility for each working system even for cellular so if you wish to study extra about Fyne or in case you are new to Go then you’ll want to cowl Go and Fyne first.



Packages used

The packages we’re going to use on this program are talked about beneath:

  • Fyne for creating GUI and it’s all Parts.
  • GoValuate offers help for evaluating arbitrary C-like artithmetic/string expressions so we will get the results of used expression.



Fyne Bundle

Fyne is an easy-to-use UI toolkit and app API written in Go. It’s designed to construct purposes that run on desktop and cellular gadgets with a single codebase.

To develop apps utilizing Fyne you will have Go model 1.14 or later, a C compiler and your system’s growth instruments. In case you’re undecided if that’s all put in otherwise you don’t know the way then try Getting Began doc.



Code

bundle predominant

import (
    "strconv"

    "fyne.io/fyne/v2/app"
    "fyne.io/fyne/v2/container"
    "fyne.io/fyne/v2/widget"
    "github.com/Knetic/govaluate"
)

func predominant() {
    myApp := app.New()
    myWindow := myApp.NewWindow("Calculator")

    mainResult := widget.NewLabel("")

    mainS := ""
    mainResult.SetText(mainS)

    one := widget.NewButton("1", func() {
        mainS += "1"
    })
    two := widget.NewButton("2", func() {
        mainS += "2"
    })
    three := widget.NewButton("3", func() {
        mainS += "3"
    })
    4 := widget.NewButton("4", func() {
        mainS += "4"
    })
    5 := widget.NewButton("5", func() {
        mainS += "5"
    })
    six := widget.NewButton("6", func() {
        mainS += "6"
    })
    seven := widget.NewButton("7", func() {
        mainS += "7"
    })
    eigth := widget.NewButton("8", func() {
        mainS += "8"
    })
    9 := widget.NewButton("9", func() {
        mainS += "9"
    })
    zero := widget.NewButton("0", func() {
        mainS += "0"
    })

    clear := widget.NewButton("C", func() {
        mainS = ""
    })

    plus := widget.NewButton("+", func() {
        mainS += "+"
    })

    minus := widget.NewButton("-", func() {
        mainS += "-"
    })

    multiply := widget.NewButton("*", func() {
        mainS += "*"
    })

    divide := widget.NewButton("https://dev.to/", func() {
        mainS += "https://dev.to/"
    })

    #fourth
    equal := widget.NewButton("=", func() {
        expression, err := govaluate.NewEvaluableExpression(mainS)
        if err == nil {
            outcome, err := expression.Consider(nil)
            if err == nil {
                mainS = strconv.FormatFloat(outcome.(float64), 'f', -1, 64)
            } else {
                mainS = err.Error()
            }
        } else {
            mainS = err.Error()
        }
    })

    #5 updating it every time
    go func() {
        for {
            mainResult.SetText(mainS)
        }
    }()

    #creating format from the given buttons
    myWindow.SetContent(container.NewVBox(
        mainResult,
        container.NewGridWithColumns(3,
            divide,
            multiply,
            minus,
            // plus,
        ),
        container.NewGridWithColumns(2,
            container.NewGridWithRows(3,
                container.NewGridWithColumns(3,
                    seven,
                    eigth,
                    9,
                ),
                container.NewGridWithColumns(3,
                    4,
                    5,
                    six,
                ),
                container.NewGridWithColumns(3,
                    one,
                    two,
                    three,
                ),
            ),
            plus,
        ),
        container.NewGridWithColumns(2,
            container.NewGridWithColumns(2,
                clear,
                zero,
            ),
            equal,
        ),
    ),
    )
    myWindow.ShowAndRun()
}
Enter fullscreen mode

Exit fullscreen mode



Output

golang-gui-calculator

golang-gui-calculator

ProgramminGeeksClub.com

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments