Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
October 23, 2022 01:49 pm GMT

Golang GUI Calculator

In this article you will learn, how you can build a calculator from scratch. Well focus on the Go and its Fyne package which helps to create GUI based application for every operating system even for mobile so if you want to learn more about Fyne or if you are new to Go then you need to cover Go and Fyne first.

Packages used

The packages we are going to use in this program are mentioned below:

  • Fyne for creating GUI and its all Components.
  • GoValuate provides support for evaluating arbitrary C-like artithmetic/string expressions so we can get the result of used expression.

Fyne Package

Fyne is an easy-to-use UI toolkit and app API written in Go. It is designed to build applications that run on desktop and mobile devices with a single codebase.

To develop apps using Fyne you will need Go version 1.14 or later, a C compiler and your systems development tools. If youre not sure if thats all installed or you dont know how then check out Getting Started document.

Code

package mainimport (    "strconv"    "fyne.io/fyne/v2/app"    "fyne.io/fyne/v2/container"    "fyne.io/fyne/v2/widget"    "github.com/Knetic/govaluate")func main() {    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"    })    four := widget.NewButton("4", func() {        mainS += "4"    })    five := 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"    })    nine := 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("/", func() {        mainS += "/"    })    #fourth    equal := widget.NewButton("=", func() {        expression, err := govaluate.NewEvaluableExpression(mainS)        if err == nil {            result, err := expression.Evaluate(nil)            if err == nil {                mainS = strconv.FormatFloat(result.(float64), 'f', -1, 64)            } else {                mainS = err.Error()            }        } else {            mainS = err.Error()        }    })    #five updating it each time    go func() {        for {            mainResult.SetText(mainS)        }    }()    #creating layout 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,                    nine,                ),                container.NewGridWithColumns(3,                    four,                    five,                    six,                ),                container.NewGridWithColumns(3,                    one,                    two,                    three,                ),            ),            plus,        ),        container.NewGridWithColumns(2,            container.NewGridWithColumns(2,                clear,                zero,            ),            equal,        ),    ),    )    myWindow.ShowAndRun()}

Output

golang-gui-calculator

golang-gui-calculator

ProgramminGeeksClub.com


Original Link: https://dev.to/mavensingh/golang-gui-calculator-2e49

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To