drogon982007 2019-08-31 10:18
浏览 58

编写确认(是/否)对话框

I am trying to write a yes/no dialog box in Go using andlabs/ui which does not seem to have this type of GUI component:

package main

import "github.com/andlabs/ui"

func confirm(msg string) bool {
    var confirmWindow = ui.NewWindow("Please confirm", 300, 100, false)
    var vbox = ui.NewVerticalBox()
    vbox.Append(ui.NewLabel(msg), false)
    var yesButton = ui.NewButton("Yes")
    yesButton.OnClicked(func (*ui.Button) {
        return true
    })
    var noButton = ui.NewButton("No")
    noButton.OnClicked(func (*ui.Button) {
        return false // <<< this should return out of the confirm function. 
    })
    var hbox = ui.NewHorizontalBox()
    hbox.Append(yesButton, false)
    hbox.Append(noButton, false)
    vbox.Append(hbox, false)
    confirmWindow.SetChild(vbox)
    confirmWindow.Show()
}

func main(){
    var ret = confirm("Proceed?")
    if ret {
        println("Yes returned.")
    } else {
        println("No returned.")
    }
}

However, in above I am not able to return true or false depending on whether yes or no button is clicked.

How can I have true/false returned from confirm function when yes/no button is clicked?

Edit: Following suggestion from answer by @Mikhail , I tried following code:

package main
import ("github.com/andlabs/ui")
func confirm(msg string) chan bool {
    var confirmWindow = ui.NewWindow("Please confirm", 300, 100, false)
    var vbox = ui.NewVerticalBox()
    vbox.Append(ui.NewLabel(msg), false)
    var yesButton = ui.NewButton("Yes")
    ret := make(chan bool)
    yesButton.OnClicked(func (*ui.Button){
        ret <- true
    })
    var noButton = ui.NewButton("No")
    noButton.OnClicked(func (*ui.Button){
        ret <- false
    })
    var hbox = ui.NewHorizontalBox()
    hbox.Append(yesButton, false)
    hbox.Append(noButton, false)
    vbox.Append(hbox, false)
    confirmWindow.SetChild(vbox)
    confirmWindow.Show()
    confirmWindow.OnClosing(func(*ui.Window)bool{
        ui.Quit(); return true
    })
    return ret
}
func main(){
    c := make(chan bool)
    c <- confirm("Proceed?")
    if c {println("Yes")} else {println("No")}
}

However, it also does not work. The error is:

# command-line-arguments
./rnconfirm.go:30:4: cannot use confirm("Proceed?") (type chan bool) as type bool in send
./rnconfirm.go:31:2: non-bool c (type chan bool) used as if condition

Using following main function also does not work:

func main(){
    println(confirm("Proceed"))
}

Error is:

(process:8577): GLib-CRITICAL **: g_ptr_array_add: assertion 'rarray' failed
fatal error: unexpected signal during runtime execution
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x7fdeb996ae19]

runtime stack:
runtime.throw(0x4bd337, 0x2a)
        /usr/local/go/src/runtime/panic.go:616 +0x81
runtime.sigpanic()
        /usr/local/go/src/runtime/signal_unix.go:372 +0x28e

goroutine 1 [syscall, locked to thread]:
...
...

Second attempt with channels is also not working:

package main
import ("github.com/andlabs/ui")
func confirm(msg string, ret chan bool) {
    var confirmWindow = ui.NewWindow("Please confirm", 300, 100, false)
    var vbox = ui.NewVerticalBox()
    vbox.Append(ui.NewLabel(msg), false)
    var yesButton = ui.NewButton("Yes")
    yesButton.OnClicked(func (*ui.Button){
        ret <- true
    })
    var noButton = ui.NewButton("No")
    noButton.OnClicked(func (*ui.Button){
        ret <- false
    })
    var hbox = ui.NewHorizontalBox()
    hbox.Append(yesButton, false)
    hbox.Append(noButton, false)
    vbox.Append(hbox, false)
    confirmWindow.SetChild(vbox)
    confirmWindow.Show()
    confirmWindow.OnClosing(func(*ui.Window)bool{
        ui.Quit(); return true
    })
}
func main(){
    c := make(chan bool)
    go confirm("Proceed?", c)
    ret := <- c
    if ret {println("Yes")} else {println("No")}
}

It compiles and runs but crashes with error:

(process:2407): GLib-CRITICAL **: g_ptr_array_add: assertion 'rarray' failed
fatal error: unexpected signal during runtime execution
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x7f47561e5e19]
  • 写回答

1条回答 默认 最新

  • dongyi9082 2019-08-31 11:26
    关注

    In the code you are returning true or false not from your function confirm but from the callbacks attached by OnClicked method to the yesButton and noButton.

    yesButton.OnClicked(func (*ui.Button) {
        return true
    })
    var noButton = ui.NewButton("No")
    noButton.OnClicked(func (*ui.Button) {
        return false // <<< this should return out of the confirm function. 
    })
    

    To communicate main thread with the callbacks you have attached you need to have some kind of communication object and in case of the golang channel would work fine. Declare chan bool in the body of the confirm function, use it through the closure in the callbacks, return it as a result of confirm funcrion and read from it in the your main function.

    TL;DR:

    • return chan bool from confirm

    • read the result from it

    • instead of trying to return bool value from callbacks, send the desired value into the channel.

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?