I find following code works:
// modified from: https://github.com/andlabs/ui/wiki/Getting-Started
package main
import ("github.com/andlabs/ui")
func makewinfn() {
var name = ui.NewEntry()
var button = ui.NewButton("Greet")
var greeting = ui.NewLabel("")
box := ui.NewVerticalBox()
box.Append(ui.NewLabel("Enter your name:"), false)
box.Append(name, false)
box.Append(button, false)
box.Append(greeting, false)
mywindow := ui.NewWindow("MyTitle", 200, 100, false)
mywindow.SetChild(box)
button.OnClicked( func (*ui.Button) {greeting.SetText("Hello, " + name.Text() + "!") } )
mywindow.OnClosing( func (*ui.Window) bool { ui.Quit(); return true } )
mywindow.Show()
}
func main() {
ui.Main(makewinfn)
}
However, if I try with global variables:
package main
import ("github.com/andlabs/ui")
// keeping following as global variables:
var name = ui.NewEntry()
var button = ui.NewButton("Greet")
var greeting = ui.NewLabel("")
func makewinfn() {
box := ui.NewVerticalBox()
box.Append(ui.NewLabel("Enter your name:"), false)
box.Append(name, false)
box.Append(button, false)
box.Append(greeting, false)
mywindow := ui.NewWindow("MyTitle", 200, 100, false)
mywindow.SetChild(box)
button.OnClicked( func (*ui.Button) {greeting.SetText("Hello, " + name.Text() + "!") } )
mywindow.OnClosing( func (*ui.Window) bool { ui.Quit(); return true } )
mywindow.Show()
}
func main() {
ui.Main(makewinfn)
}
This code with global variables compiles all right but creates following error on running:
fatal error: unexpected signal during runtime execution
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x7fecb2712e19]
How can I keep GUI components as global variables? I have keep them as global so that I can access them from other functions.