从一组代码中同时打开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.

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")
}

I don't know Go but assuming the GUI works like any other language I've used, in your makewinfn function you can simply create more windows by calling ui.NewWindow() again.

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()
    myOtherWindow := ui.NewWindow("MyOtherTitle", 200, 100, false)
    myOtherWindow.Show()
}