Search code examples
goimportgtk

GTK widgets not recognized in Go using github.com/gotk3/gotk3


(I am not a new user - long time SO participant but due to inactivity old profile was lost)

Doing some experiments with GoLang and GTK, using github.com/gotk3/gotk3 - using Liteide on Windows 11.

Copied the following code, based on sample code from github.com/gotk3/gotk:

package main

import (
    "log"
    "os"

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

func main() {

    const appID = "org.gtk.example"
    application, err := gtk.ApplicationNew(appID, glib.APPLICATION_FLAGS_NONE)
    
    if err != nil {
        log.Fatal("Could not create application.", err)
    }
    application.Connect("activate", func() { onActivate(application) })
    // Run Gtk application
    os.Exit(application.Run(os.Args))
}


func onActivate(application *gtk.Application) {

    appWindow, err := gtk.ApplicationWindowNew(application)
    if err != nil {
        log.Fatal("Could not create application window.", err)
    }

    l, err := gtk.LabelNew("Hello, gotk3!")
    if err != nil {
        log.Fatal("Unable to create label:", err)
    }

    appWindow.Add(l)
    appWindow.SetTitle("Basic Application.")
    appWindow.SetDefaultSize(800, 400)
    appWindow.Show()

}

The sample code compiles and runs fine. But I added these three lines to the sample code:

    vbox := gtk.NewVBox(false, 1)
    menubar := gtk.NewMenuBar()
    vbox.PackStart(menubar, false, false, 0)

Now when building, I get errors:

\main.go:51:14: undefined: gtk.NewVBox
\main.go:56:17: undefined: gtk.NewMenuBar 

My go.mod file seems to be OK:

module goGTK

go 1.21.4

require github.com/gotk3/gotk3 v0.6.2

go module tidy imported the necessary GTK modules. The program compiles and runs fine without my additions.

Why are gtk.NewVBox and gtk.NewMenuBar not recognized?


Solution

  • Seems the problem was with GTK versions:

    gtk.NewVBox() and gtk.NewMenuBar() are apparently from GTK 2.

    Go GTK wrapper expects GTK 3. gtk.BoxNew() and gtk.MenuBarNew() from GTK 3 work fine.