小能豆

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

go

(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?


阅读 66

收藏
2023-12-18

共1个答案

小能豆

The code you provided is based on an older version of the gotk3 library. In the newer versions, the way you create containers and widgets has changed. The functions gtk.NewVBox and gtk.NewMenuBar have been replaced with different methods and constructors.

Here’s how you can modify your code to work with the latest version of gotk3:

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)
    }

    vbox, err := gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 0)
    if err != nil {
        log.Fatal("Unable to create vbox:", err)
    }

    menubar, err := gtk.MenuBarNew()
    if err != nil {
        log.Fatal("Unable to create menubar:", err)
    }

    vbox.PackStart(menubar, false, false, 0)

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

    vbox.PackStart(l, false, false, 0)

    appWindow.Add(vbox)
    appWindow.SetTitle("Basic Application.")
    appWindow.SetDefaultSize(800, 400)
    appWindow.ShowAll()
}

In this modified code:

  1. I replaced gtk.NewVBox with gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 0) to create a vertical box.
  2. I replaced gtk.NewMenuBar with gtk.MenuBarNew() to create a menu bar.
  3. I added vbox.PackStart(...) for both the menubar and label to pack them into the vertical box.

These changes should resolve the compilation errors, and your program should work with the latest version of gotk3.

2023-12-18