Prerequisites

Before we start, ensure you have the following installed:

  • Go (version 1.13 or higher)
  • A text editor or IDE (like Visual Studio Code or GoLand)
  • Basic knowledge of Go programming

Setting Up the Fyne Library

First, we need to install the Fyne library. Open your terminal and run the following command:

go get fyne.io/fyne/v2

This command will download the Fyne library and add it to your Go module dependencies.

Creating Your First Fyne Application

Now let's create a simple GUI application that displays a window with a button. When the button is clicked, it will show a message.

  1. Create a new directory for your project and navigate into it:
   mkdir my-fyne-app
   cd my-fyne-app
  1. Create a new Go file named main.go:
   touch main.go
  1. Open main.go in your text editor and add the following code:
   package main

   import (
       "fyne.io/fyne/v2/app"
       "fyne.io/fyne/v2/container"
       "fyne.io/fyne/v2/widget"
       "fyne.io/fyne/v2/dialog"
   )

   func main() {
       // Create a new application
       myApp := app.New()
       myWindow := myApp.NewWindow("My Fyne App")

       // Create a button
       button := widget.NewButton("Click Me", func() {
           dialog.ShowInformation("Information", "Button Clicked!", myWindow)
       })

       // Set the content of the window
       myWindow.SetContent(container.NewVBox(button))

       // Show and run the application
       myWindow.ShowAndRun()
   }

Code Explanation

  • Importing Packages: We import necessary packages from the Fyne library to create the application, window, container, button, and dialog.
  • Creating the Application: We initialize a new Fyne application using app.New() and create a window with a title.
  • Creating a Button: We create a button that, when clicked, will display an informational dialog.
  • Setting Window Content: We set the content of the window to a vertical box container that holds the button.
  • Running the Application: Finally, we call ShowAndRun() to display the window and start the application event loop.

Running the Application

To run your application, execute the following command in your terminal:

go run main.go

You should see a window appear with a button labeled "Click Me." When you click the button, an information dialog will pop up, displaying the message "Button Clicked!"

Enhancing the Application

Now that we have a basic application, let's enhance it by adding more functionality, such as input fields and labels.

  1. Modify the code in main.go:
   package main

   import (
       "fyne.io/fyne/v2/app"
       "fyne.io/fyne/v2/container"
       "fyne.io/fyne/v2/widget"
       "fyne.io/fyne/v2/dialog"
   )

   func main() {
       myApp := app.New()
       myWindow := myApp.NewWindow("My Enhanced Fyne App")

       // Create input field
       entry := widget.NewEntry()
       entry.SetPlaceHolder("Enter your name")

       // Create a button that greets the user
       button := widget.NewButton("Greet Me", func() {
           name := entry.Text
           dialog.ShowInformation("Greeting", "Hello, "+name+"!", myWindow)
       })

       // Set the content of the window
       myWindow.SetContent(container.NewVBox(entry, button))

       // Show and run the application
       myWindow.ShowAndRun()
   }

New Features Explained

  • Input Field: We added an entry widget for user input, allowing users to enter their names.
  • Greeting Button: The button now greets the user by name when clicked, using the text from the input field.

Conclusion

In this tutorial, we covered the basics of building a simple GUI application using the Fyne library in Go. We started with a basic application and then enhanced it by adding user input functionality. Fyne allows for more complex interfaces, including layouts, images, and more, which can be explored further.

Learn more with useful resources