dontpanic92 / wxGo

Golang wxWidgets Wrapper
Other
403 stars 51 forks source link

Can't get WebView started in wx.Frame #41

Closed salmoni closed 6 years ago

salmoni commented 6 years ago

I've written a small program to check out the working of the WebView but have failed to get it going after trying a fair few options. Here is some basic code (cribbed from dapeton). I'm learning Go so am probably missing something basic.

package main

import "github.com/dontpanic92/wxGo/wx"

//Frame Struct def
type MyFrame struct {
    wx.Frame
    myhtml wx.WebView
}

//Frame Init def
func NewMyFrame() *MyFrame {
    f := &MyFrame{}
    f.Frame = wx.NewFrame(wx.NullWindow, -1, "Brwsrrr")
    mainSizer := wx.NewBoxSizer(wx.HORIZONTAL )
    f.SetSizer(mainSizer)

    f.myhtml.Create()
    mainSizer.Add(f.myhtml, 1, wx.EXPAND, 5)
    f.myhtml.LoadURL("http://www.salstat.com")

    f.Layout()

    return f
}

//Main Function
func main() {
    wx1 := wx.NewApp()
    f := NewMyFrame()
    f.Show()
    wx1.MainLoop()
    return
}

This builds but running the executable produces:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x140 pc=0x431a254]

goroutine 1 [running]:
main.NewMyFrame(0x0)
/Users/alan/Projects/GSalstat/WebViewTest/WebViewTest.go:18 +0x1b4
main.main()
/Users/alan/Projects/GSalstat/WebViewTest/WebViewTest.go:30 +0x59

I've tried using f.myhtml = NewWebView() but that's not building. What am I missing here?

Platform: OSX 10.12.6 Go: 1.9.2 wxGo: Downloaded over the weekend.

dontpanic92 commented 6 years ago

Replace the f.myhtml.Create() with f.myhtml = wx.WebViewNew(f, wx.ID_ANY)

Functions cannot be called before the object has been initialized

salmoni commented 6 years ago

Excellent, thank you! That works a treat.

I was thrown out by the WebViewNew – I tried using NewWebView which failed.

Thanks again.