Designing cross-platform GUI apps with Fyne

Platform Agnostic

© Lead Image © Ion Chiosea, 123RF.com

© Lead Image © Ion Chiosea, 123RF.com

Article from Issue 273/2023
Author(s):

The Fyne toolkit offers a simple way to build native apps that work across multiple platforms. We show you how to build a to-do list app to demonstrate Fyne's power.

One of the biggest challenges in modern app development is the requirement to develop a separate codebase for each platform that you want to support. Fortunately, the Fyne graphical user interface (GUI) toolkit lets you program your app once and then deploy it rapidly to multiple devices.

Fyne is an open source GUI toolkit built for the Go programming language, a popular general-purpose compiled programming language created by Google a little over 10 years ago. Go is frequently used for cloud services and infrastructure projects. However, the Fyne toolkit makes it possible to quickly build native GUIs for desktop and mobile devices. One of a number of GUI toolkits for Go, Fyne aims to be the simplest to use, appealing to both beginners and experienced GUI developers.

Fyne's idiomatic design makes it easy for Go developers to rapidly learn its principles. Fyne allows developers to create their own custom widgets thanks to its rich widget set and extensibility. With graphical elements inspired by Material Design, Fyne will feel familiar to many end users, while remaining open to theming by app developers. Fyne is fully unit tested and supports rigorous test-driven development. Released under the same permissive BSD licence as Go, Fyne is supported by an active development community.

In this article, I will show you how to set up your system to program your first app with Go and Fyne. I will cover the basic APIs for showing windows, laying out content, and using the standard widgets, along with storing data and working with event handlers. By the end of this article, you will have a complete graphical application running on your computer and smartphone device.

Setup

To get started with Fyne, you need to install a few developer tools including Git, Go, and a C compiler. Assuming you are working on a Linux desktop with an X11 setup, you also need to install some libraries. Don't worry – this only applies to developing Fyne apps. Your users won't need any of these dependencies to run the finished apps. Fyne (like Go) focuses on single binary compiler output with zero dependencies. As long as your users have a working graphics driver, your apps will work without any additional setup.

You should use your computer's package manager to install the necessary tools. All distributions are different, but the most common systems will need to use the following commands. For Debian and Ubuntu, use:

sudo apt-get install golang gcc libgl1-mesa-dev xorg-dev

On Fedora, use:

sudo dnf install golang gcc libXcursor-devel libXrandr-develmesa-libGL-devel libXi-devellibXinerama-devel libXxf86vm-devel

For Arch Linux, use:

sudo pacman -S go xorg-server-devel libxcursor libxrandr libxinerama libxi

You also need at least Go v1.14 for Fyne code to compile. You can adapt the above commands for your package manager or find the full list in the Fyne documentation [1].

Getting Started

Once you've installed the necessary tools, you are now ready to build a basic Go app to make sure that everything is working. First, make a new folder and set it up as a Go project using go mod init (Listing 1).

Listing 1

Create a Project

> mkdir todoapp
> cd todoapp
> go mod init todoapp
go: creating new go.mod: module todoapp

You now have a folder that contains a go.mod file. You can test to see if the Go compiler is working by quickly sending some basic file content to main.go and then building the app (Listing 2). All you need is the package name (main) and a simple main() function to let the compiler know you are building an application instead of a library.

Listing 2

Test the Install

:> echo "package main
func main() {}" > main.go
> go build

If your build completes without error, then it has succeeded. You will see a file called todoapp. You can run todoapp at this stage, but you will not get output because you have not built your app yet!

Showing a Window

I'll now demonstrate how to write some code to display a window onscreen. First, you need to install the Fyne library into your project with go get. The following command will download the Fyne project's source code from GitHub and store it in the Go cache. It then marks Fyne as a dependency of your project so that your code editor or IDE can make sensible suggestions for autocompletion and compilation:

> go get fyne.io/fyne/v2@latest
go: added fyne.io/fyne/v2 v2.3.4

After installing the Fyne library, you next must create a new application instance, which is the variable that will be used to create new windows, access storage, and use other important features. You can create a new app by inserting the following into your main func:

a := app.New()

However, it is best practice to identify your app, which can be done using:

app.NewWithID("com.example.myapp")

The code should be added to the top of your main function. This app package is from the import path fyne.io/fyne/v2/app (your IDE will probably automatically add this).

In the new app, you can create a new window by simply requesting it with:

w := app.NewWindow("TODO")

This sets up the variable w for the new window. The window will have the title "TODO" which may be displayed in your window border. At this point, you could build the app. However, without any content, the window would be fairly useless, so I'll show you how to add a widget.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Wheat and Chaff

    If you want to keep only the good photos from your digital collection, you have to find and delete the fails. Mike Schilli writes a graphical application with Go and the Fyne framework to help you cull your photo library.

  • Treasure Hunt

    A geolocation guessing game based on the popular Wordle evaluates a player's guesses based on the distance from and direction to the target location. Mike Schilli turns this concept into a desktop game in Go using the photos from his private collection.

  • Chip Shot

    We all know that the Fyne framework for Go can be used to create GUIs for the desktop, but you can also write games with it. Mike Schilli takes on a classic from the soccer field.

  • Straight to the Point

    With the Fyne framework, Go offers an easy-to-use graphical interface for all popular platforms. As a sample application, Mike uses an algorithm to draw arrows onto images.

  • Magic Cargo

    To be able to power up and shut down his NAS and check the current status without getting out of his chair, Mike Schilli programs a graphical interface that sends a Magic Packet in this month's column.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News