Created
February 19, 2018 20:40
-
-
Save grandchild/690b42ae4c0673954001f8c96ec53d09 to your computer and use it in GitHub Desktop.
gotk glib.TimeoutAdd() single execution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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