duanmu2941 2019-08-13 15:25
浏览 75
已采纳

从一组代码中同时打开2个相同的窗口

I am using following code to create and show a window with GUI components as label, entry and button:

// 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)
    // HOW TO CREATE AND SHOW ANOTHER SUCH WINDOW HERE ?
    // ui.Main(makewinfn) // this opens window only after first is closed.
}

It works well, but as I mentioned in title and as commented in code above, how can I open two such windows simultaneously from main function?

Repeating ui.Main(makewinfn) in main function leads to second window opening only after first is closed.

Thanks for your help.

  • 写回答

2条回答 默认 最新

  • duanbei1709 2019-08-13 19:22
    关注

    In UI libraries usually the components or widgets you build the interface with have parents, and usually a single component may have at most one parent.

    So if you want 2 windows, having the same components, you still have to create those components in 2 instances, because a component cannot be added to 2 different parents (in 2 different windows).

    So simplest would be to move the component and window creation logic into a function, and call that twice from the function you pass to ui.Main():

    func createWindow() {
        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 makewinfn() {
        createWindow()
        createWindow()
    }
    

    Using this createWindow() function of course is not a requirement, you could have a loop in makewinfn() with 2 iterations, each which could create a window.

    The above example creates 2 identical windows, but they will be "independent". If you enter a text in one of them and click on its button, the result will only be seen in its containing / parent window. This is possible because each component has been created twice.

    If you wan to customize the windows, you could pass a parameter to createWindow() so the window and its content could be customized / personalized based on its value. For example:

    func createWindow(id string) {
        var name = ui.NewEntry()
        var button = ui.NewButton("Greet " + id)
        var greeting = ui.NewLabel("")
        box := ui.NewVerticalBox()
        box.Append(ui.NewLabel("Enter your name " + id + ":"), false)
        box.Append(name, false)
        box.Append(button, false)
        box.Append(greeting, false)
        mywindow := ui.NewWindow("MyTitle " + id, 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 makewinfn() {
        createWindow("one")
        createWindow("two")
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站