Convert Figma logo to code with AI

gotk3 logogotk3

Go bindings for GTK3

2,089
229
2,089
119

Top Related Projects

24,874

Cross platform GUI toolkit in Go inspired by Material Design

10,442

Qt binding for Go (Golang) with support for Windows / macOS / Linux / FreeBSD / Android / iOS / Sailfish OS / Raspberry Pi / AsteroidOS / Ubuntu Touch / JavaScript / WebAssembly

8,335

Platform-native GUI library for Go.

Quick Overview

gotk3 is a Go bindings library for GTK3, providing access to the GTK3 and GDK3 libraries in Go. It allows developers to create graphical user interfaces (GUIs) for desktop applications using Go, leveraging the power and flexibility of the GTK toolkit.

Pros

  • Native Go implementation, allowing for seamless integration with Go projects
  • Provides access to a wide range of GTK3 widgets and functionality
  • Active community and ongoing development
  • Cross-platform support (Linux, Windows, macOS)

Cons

  • Learning curve for developers not familiar with GTK
  • Documentation can be sparse in some areas
  • Occasional API changes may require code updates
  • Performance may be slightly lower compared to native GTK implementations

Code Examples

  1. Creating a simple window:
package main

import (
    "log"

    "github.com/gotk3/gotk3/gtk"
)

func main() {
    gtk.Init(nil)

    win, err := gtk.WindowNew(gtk.WINDOW_TOPLEVEL)
    if err != nil {
        log.Fatal("Unable to create window:", err)
    }

    win.SetTitle("Simple Window")
    win.Connect("destroy", func() {
        gtk.MainQuit()
    })

    win.ShowAll()
    gtk.Main()
}
  1. Adding a button to the window:
button, err := gtk.ButtonNewWithLabel("Click Me")
if err != nil {
    log.Fatal("Unable to create button:", err)
}

button.Connect("clicked", func() {
    log.Println("Button clicked!")
})

win.Add(button)
  1. Creating a simple dialog:
dialog, err := gtk.DialogNewWithButtons(
    "My Dialog",
    win,
    gtk.DIALOG_MODAL,
    []interface{}{
        "OK", gtk.RESPONSE_OK,
        "Cancel", gtk.RESPONSE_CANCEL,
    },
)
if err != nil {
    log.Fatal("Unable to create dialog:", err)
}

content, _ := dialog.GetContentArea()
label, _ := gtk.LabelNew("This is a dialog")
content.Add(label)

dialog.ShowAll()
response := dialog.Run()
dialog.Destroy()

log.Printf("Dialog response: %d", response)

Getting Started

  1. Install Go (1.16+ recommended)
  2. Install GTK3 development libraries for your OS
  3. Install gotk3:
    go get github.com/gotk3/gotk3/gtk
    
  4. Create a new Go file (e.g., main.go) and add the simple window example from above
  5. Run the application:
    go run main.go
    

Competitor Comparisons

24,874

Cross platform GUI toolkit in Go inspired by Material Design

Pros of Fyne

  • Pure Go implementation, no CGo dependencies
  • Cross-platform support with a single codebase
  • Modern, material design-inspired look and feel

Cons of Fyne

  • Less mature and fewer widgets compared to GTK
  • Limited access to native OS features
  • Steeper learning curve for developers familiar with traditional GUI toolkits

Code Comparison

Fyne example:

package main

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

func main() {
    myApp := app.New()
    myWindow := myApp.NewWindow("Hello")
    myWindow.SetContent(widget.NewLabel("Hello Fyne!"))
    myWindow.ShowAndRun()
}

Gotk3 example:

package main

import (
    "github.com/gotk3/gotk3/gtk"
    "log"
)

func main() {
    gtk.Init(nil)
    win, err := gtk.WindowNew(gtk.WINDOW_TOPLEVEL)
    if err != nil {
        log.Fatal("Unable to create window:", err)
    }
    win.SetTitle("Hello")
    win.Connect("destroy", func() {
        gtk.MainQuit()
    })
    win.ShowAll()
    gtk.Main()
}

Both examples create a simple window with a label, showcasing the different approaches and syntax between Fyne and Gotk3.

10,442

Qt binding for Go (Golang) with support for Windows / macOS / Linux / FreeBSD / Android / iOS / Sailfish OS / Raspberry Pi / AsteroidOS / Ubuntu Touch / JavaScript / WebAssembly

Pros of qt

  • Cross-platform support for desktop and mobile (iOS, Android)
  • Extensive set of UI components and widgets
  • Integrated development environment (Qt Creator) for easier GUI design

Cons of qt

  • Larger binary size and resource usage
  • Steeper learning curve, especially for Go developers
  • Licensing considerations for commercial applications

Code Comparison

gotk3 example:

window, _ := gtk.WindowNew(gtk.WINDOW_TOPLEVEL)
button, _ := gtk.ButtonNewWithLabel("Click Me")
window.Add(button)
window.ShowAll()

qt example:

app := widgets.NewQApplication(len(os.Args), os.Args)
window := widgets.NewQMainWindow(nil, 0)
button := widgets.NewQPushButton2("Click Me", nil)
window.SetCentralWidget(button)
window.Show()

Both libraries provide ways to create GUI applications in Go, but qt offers more extensive features and cross-platform support, while gotk3 is more lightweight and closely follows the GTK+ API. The choice between them depends on project requirements, target platforms, and developer preferences.

8,335

Platform-native GUI library for Go.

Pros of ui

  • Cross-platform native GUI toolkit with a simpler API
  • Lightweight and focused on basic UI elements
  • Easier to get started for beginners

Cons of ui

  • Limited widget set compared to GTK+
  • Less mature and actively maintained
  • Fewer advanced features and customization options

Code Comparison

ui:

window := ui.NewWindow("Hello", 200, 100, false)
button := ui.NewButton("Click Me")
window.SetChild(button)
window.OnClosing(func(*ui.Window) bool {
    ui.Quit()
    return true
})

gotk3:

win, _ := gtk.WindowNew(gtk.WINDOW_TOPLEVEL)
win.SetTitle("Hello")
win.Connect("destroy", gtk.MainQuit)
button, _ := gtk.ButtonNewWithLabel("Click Me")
win.Add(button)
win.SetDefaultSize(200, 100)

ui provides a more straightforward API for creating basic GUI applications, while gotk3 offers more extensive GTK+ bindings with greater flexibility and feature set. ui is better suited for simple, cross-platform applications, whereas gotk3 is more appropriate for complex GUI projects leveraging the full power of GTK+.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

gotk3 GoDoc

Build Status

The gotk3 project provides Go bindings for GTK 3 and dependent projects. Each component is given its own subdirectory, which is used as the import path for the package. Partial binding support for the following libraries is currently implemented:

  • GTK 3 (3.12 and later)
  • GDK 3 (3.12 and later)
  • GLib 2 (2.36 and later)
  • Cairo (1.10 and later)

Care has been taken for memory management to work seamlessly with Go's garbage collector without the need to use or understand GObject's floating references.

for better understanding see package reference documation

On Linux, see which version your distribution has here with the search terms:

  • libgtk-3
  • libglib2
  • libgdk-pixbuf2

Sample Use

The following example can be found in Examples.

package main

import (
    "github.com/gotk3/gotk3/gtk"
    "log"
)

func main() {
    // Initialize GTK without parsing any command line arguments.
    gtk.Init(nil)

    // Create a new toplevel window, set its title, and connect it to the
    // "destroy" signal to exit the GTK main loop when it is destroyed.
    win, err := gtk.WindowNew(gtk.WINDOW_TOPLEVEL)
    if err != nil {
        log.Fatal("Unable to create window:", err)
    }
    win.SetTitle("Simple Example")
    win.Connect("destroy", func() {
        gtk.MainQuit()
    })

    // Create a new label widget to show in the window.
    l, err := gtk.LabelNew("Hello, gotk3!")
    if err != nil {
        log.Fatal("Unable to create label:", err)
    }

    // Add the label to the window.
    win.Add(l)

    // Set the default window size.
    win.SetDefaultSize(800, 600)

    // Recursively show all widgets contained in this window.
    win.ShowAll()

    // Begin executing the GTK main loop.  This blocks until
    // gtk.MainQuit() is run.
    gtk.Main()
}

To build the example:

$ go build example.go

To build this example with older gtk version you should use gtk_3_10 tag:

$ go build -tags gtk_3_10 example.go

Example usage

package main

import (
    "log"
    "os"

    "github.com/gotk3/gotk3/glib"
    "github.com/gotk3/gotk3/gtk"
)

// Simple Gtk3 Application written in go.
// This application creates a window on the application callback activate.
// More GtkApplication info can be found here -> https://wiki.gnome.org/HowDoI/GtkApplication

func main() {
    // Create Gtk Application, change appID to your application domain name reversed.
    const appID = "org.gtk.example"
    application, err := gtk.ApplicationNew(appID, glib.APPLICATION_FLAGS_NONE)
    // Check to make sure no errors when creating Gtk Application
    if err != nil {
        log.Fatal("Could not create application.", err)
    }
    // Application signals available
    // startup -> sets up the application when it first starts
    // activate -> shows the default first window of the application (like a new document). This corresponds to the application being launched by the desktop environment.
    // open -> opens files and shows them in a new window. This corresponds to someone trying to open a document (or documents) using the application from the file browser, or similar.
    // shutdown ->  performs shutdown tasks
    // Setup Gtk Application callback signals
    application.Connect("activate", func() { onActivate(application) })
    // Run Gtk application
    os.Exit(application.Run(os.Args))
}

// Callback signal from Gtk Application
func onActivate(application *gtk.Application) {
    // Create ApplicationWindow
    appWindow, err := gtk.ApplicationWindowNew(application)
    if err != nil {
        log.Fatal("Could not create application window.", err)
    }
    // Set ApplicationWindow Properties
    appWindow.SetTitle("Basic Application.")
    appWindow.SetDefaultSize(400, 400)
    appWindow.Show()
}
package main

import (
    "log"
    "os"

    "github.com/gotk3/gotk3/glib"
    "github.com/gotk3/gotk3/gtk"
)

// Simple Gtk3 Application written in go.
// This application creates a window on the application callback activate.
// More GtkApplication info can be found here -> https://wiki.gnome.org/HowDoI/GtkApplication

func main() {
    // Create Gtk Application, change appID to your application domain name reversed.
    const appID = "org.gtk.example"
    application, err := gtk.ApplicationNew(appID, glib.APPLICATION_FLAGS_NONE)
    // Check to make sure no errors when creating Gtk Application
    if err != nil {
        log.Fatal("Could not create application.", err)
    }

    // Application signals available
    // startup -> sets up the application when it first starts
    // activate -> shows the default first window of the application (like a new document). This corresponds to the application being launched by the desktop environment.
    // open -> opens files and shows them in a new window. This corresponds to someone trying to open a document (or documents) using the application from the file browser, or similar.
    // shutdown ->  performs shutdown tasks
    // Setup activate signal with a closure function.
    application.Connect("activate", func() {
        // Create ApplicationWindow
        appWindow, err := gtk.ApplicationWindowNew(application)
        if err != nil {
            log.Fatal("Could not create application window.", err)
        }
        // Set ApplicationWindow Properties
        appWindow.SetTitle("Basic Application.")
        appWindow.SetDefaultSize(400, 400)
        appWindow.Show()
    })
    // Run Gtk application
    application.Run(os.Args)
}

Documentation

Each package's internal go doc style documentation can be viewed online without installing this package by using the GoDoc site (links to cairo, glib, gdk, and gtk documentation).

You can also view the documentation locally once the package is installed with the godoc tool by running godoc -http=":6060" and pointing your browser to http://localhost:6060/pkg/github.com/gotk3/gotk3

Installation

gotk3 currently requires GTK 3.6-3.24, GLib 2.36-2.46, and Cairo 1.10 or 1.12. A recent Go (1.8 or newer) is also required.

For detailed instructions see the wiki pages: installation

Using deprecated features

By default, deprecated GTK features are not included in the build.

By specifying the e.g. build tag gtk_3_20, any feature deprecated in GTK 3.20 or earlier will NOT be available. To enable deprecated features in the build, add the tag gtk_deprecated. Example:

$ go build -tags "gtk_3_10 gtk_deprecated" example.go

The same goes for

  • gdk-pixbuf: gdk_pixbuf_deprecated

TODO

  • Add bindings for all of GTK functions
  • Add tests for each implemented binding
  • See the next steps: wiki page and add your suggestion

License

Package gotk3 is licensed under the liberal ISC License.

Actually if you use gotk3, then gotk3 is statically linked into your application (with the ISC licence). The system libraries (e.g. GTK+, GLib) used via cgo use dynamic linking.