Designing cross-platform GUI apps with Fyne
Platform Agnostic
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
(incl. VAT)