我发现以下代码有效:
// 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)
}
但是,如果我尝试使用全局变量:
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)
}
这段带有全局变量的代码可以正常编译,但在运行时会产生以下错误:
fatal error: unexpected signal during runtime execution
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x7fecb2712e19]
如何将 GUI 组件保留为全局变量?我将它们保留为全局,以便我可以从其他功能访问它们。
芜湖不芜
相关分类