Walk is a "Windows Application Library Kit" for the Go Programming Language.
Its focus is graphical user interfaces but there is some more stuff.
Make sure you have a working Go installation. See Getting Started
Now run go get github.com/lxn/walk
There are three ways to create GUIs with Walk:
-
Imperative code ==================
import ( "log" "github.com/lxn/walk" )
func main() { mw, err := walk.NewMainWindow() if err != nil { log.Fatal(err) }
if err := mw.SetTitle("My Cool App"); err != nil { log.Fatal(err) } if err := mw.SetLayout(walk.NewVBoxLayout()); err != nil { log.Fatal(err) } pb, err := walk.NewPushButton(mw) if err != nil { log.Fatal(err) } if err := pb.SetText("Don't Click Me!"); err != nil { log.Fatal(err) } pb.Clicked().Attach(func(){ panic("Ouch!") }) mw.Show() mw.Run()
}
Using this approach, a lot of error handling is required. In this code it is hard to see the hierarchical structure of the GUI.
-
Declarative code ===================
import ( "log" "github.com/lxn/walk" . "github.com/lxn/walk/declarative" )
func main() { var mw *walk.MainWindow
if err := (MainWindow{ AssignTo: &mw, Title: "My Cool App", Layout: VBox{}, Children: []Widget{ PushButton{Text: "Don't Click Me!", OnClicked: func() { panic("Ouch!") }}, }, }.Create(nil)); err != nil { log.Fatal(err) } mw.Show() mw.Run()
}
This requires much less error handling and the hierarchical structure of the GUI is reflected in the code.
- Qt Designer and ui2walk ==========================
The ui2walk tool generates Go code for use with Walk from Qt Designer ui files.
It generates .go files for every .ui file in the current working directory, recursively.
If you e.g. have a file 'mydialog.ui', ui2walk will create 'mydialog_ui.go' in the same directory. This file will get regenerated every time you run ui2walk, so don't edit it.
If it doesn't already exist, ui2walk also creates a matching "logic" file 'mydialog.go', which will not be regenerated, so you can extend it:
package main
import "github.com/lxn/walk"
type MyDialog struct {
*walk.Dialog
ui myDialogUI
}
func RunMyDialog(owner walk.RootWidget) (int, error) {
dlg := new(MyDialog)
if err := dlg.init(owner); err != nil {
return 0, err
}
// TODO: Do further required setup, e.g. for event handling, here.
return dlg.Run(), nil
}
ui2walk emits i18n-ed strings when using the -tr
flag. See
https://github.com/lxn/polyglot for more details.