dongsilu2237 2019-08-13 12:42
浏览 65
已采纳

无法将GUI变量保持为全局

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.

  • 写回答

1条回答 默认 最新

  • dongzai0020 2019-08-13 12:50
    关注

    When you use variables at the top level (package block), they are initialized before execution of main() begins.

    And you call code from the github.com/andlabs/ui package, but its ui.Main() was not yet called, and so the ui package and resources it depends on may not have been initialized.

    Only declare the variables but do not yet assign values to them, leave that to the makewinfn() function:

    var name *ui.Entry
    var button *ui.Button
    var greeting *ui.Label
    
    func makewinfn() {
        name = ui.NewEntry()
        button = ui.NewButton("Greet")
        greeting = ui.NewLabel("")
    
        box := ui.NewVerticalBox()
        // ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多