conformal / gotk3

Go bindings for GTK3
ISC License
470 stars 81 forks source link

mainwindow.ShowAll undefined (type glib.IObject has no field or method ShowAll) #101

Closed shackra closed 9 years ago

shackra commented 9 years ago

I'm trying to use a designed interface with Glade in my Go application, like this:

package main

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

func main() {
    gtk.Init(nil)
    builder, _ := gtk.BuilderNew()
    builder.AddFromFile("ui/main.glade")

    mainwindow, _ := builder.GetObject("MainWindow1")
    mainwindow.Connect("destroy", func() {
        gtk.MainQuit()
    })
    mainwindow.ShowAll()
}

But it seems that is not working, I get the following error:

# command-line-arguments
./testapp.go:13: mainwindow.Connect undefined (type glib.IObject has no field or method Connect)
./testapp.go:16: mainwindow.ShowAll undefined (type glib.IObject has no field or method ShowAll)
jrick commented 9 years ago

ShowAll is a method of gtk.Widget. You will have to type assert the mainwindow variable from a glib.IObject (an interface which can pack any object) to the actual type which implements that method. In this case that would probably be a *gtk.Window.

BTW, your example program is incorrect since it does not run the GTK event loop (with gtk.Main) and will immediately close.

davecgh commented 9 years ago

To expand on what @jrick said:

    mainwindowobj, _ := builder.GetObject("MainWindow1")
    mainwindow, ok := mainwindowobj.(*gtk.Window)
    if !ok {
        fmt.Println("wrong type")
        return
    }
    mainwindow.Connect("destroy", func() {
        gtk.MainQuit()
    })
    mainwindow.ShowAll()
shackra commented 9 years ago

Oh, oops! I need to choose the simpler window instead:

2015/01/24 00:31:08 Error at getting the Window object: unrecognized class name 'GtkApplicationWindow'

Thanks, I didn't know how to work around this issue :)