Skip to content

Instantly share code, notes, and snippets.

@grandchild
Created February 19, 2018 20:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grandchild/690b42ae4c0673954001f8c96ec53d09 to your computer and use it in GitHub Desktop.
Save grandchild/690b42ae4c0673954001f8c96ec53d09 to your computer and use it in GitHub Desktop.
gotk glib.TimeoutAdd() single execution
// This code contains no error checking for brevity.
// Always check your errors, kids!
package main
import (
"fmt"
"github.com/gotk3/gotk3/glib"
"github.com/gotk3/gotk3/gtk"
)
type Gui struct {
window *gtk.Window
label *gtk.Label
counter int
}
func main() {
gtk.Init(nil)
gui := GuiNew()
gui.window.ShowAll()
gtk.Main()
}
// Creates a window with a label showing a counter.
func GuiNew() *Gui {
win, _ := gtk.WindowNew(gtk.WINDOW_TOPLEVEL)
win.Connect("destroy", func() { gtk.MainQuit() })
label, _ := gtk.LabelNew("Test 0")
win.Add(label)
gui := Gui{win, label, 0}
glib.TimeoutAdd(500, gui.callback)
return &gui
}
// Periodically called to increase the counter on the label.
func (g *Gui) callback() bool {
g.counter += 1
g.label.SetText(fmt.Sprintf("Test %d", g.counter))
// ### This next line shouldn't be necessary... ###
glib.TimeoutAdd(500, g.callback)
// ### ... because callback() returns 'true'. ###
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment