Skip to content

Update your UI in other goroutine

Steven Zack edited this page Dec 16, 2016 · 2 revisions

From any goroutine, you can do:

ui.QueueMain(func () {
    // Update the UI here
})

Example:

ui.Main(func () {
    e := ui.NewEntry()

    go func() {
        // Do some stuff outside the UI thread

        ui.QueueMain(func () {
            e.SetText('text from goroutine')
        })
    }()
})

Here is a practice

package main

import (
	"github.com/andlabs/ui"
	"strconv"
	"time"
)

func main() {
	err := ui.Main(func() {
		window := ui.NewWindow("title", 200, 100, false)
		label := ui.NewLabel("text")
		window.SetChild(label)
		window.OnClosing(func(*ui.Window) bool {
			ui.Quit()
			return true
		})
		window.Show()
		go counter(label)
	})
	if err != nil {
		panic(err)
	}
}
func counter(label *ui.Label) {
	for i := 0; i < 5; i++ {
		time.Sleep(time.Second)
		ui.QueueMain(func() {
			label.SetText("number " + strconv.Itoa(i))
		})
	}
}