GO-グラフィックユーザインタフェース
10788 ワード
グラフィックユーザインタフェース1:初認識Walk
GoとGUI-GO言語のグラフィックインタフェースWalk
GOはオリジナルのインタフェースライブラリがないので、GUIインタフェースを直接書くことはできません.しかし、最近、インターネットには成熟した使いやすいサードパーティインタフェースライブラリがたくさん登場しています.これらを使用すると、同様にC#、C++とのインタフェースを書くことができます.しかも効率はもっと優れている.
Walkインタフェースライブラリ(公式紹介):WalkはGolangに書かれたWindowアプリケーションライブラリキットで、デスクトップGUIの開発に主に使用されていますが、もっと多くのものがあります.
Walkのインストール
このセクションを参照する前に、go言語のインストールが完了した環境が構成されていることを確認してください.ない場合はgo公式サイトのInstallヘルプを参照してください.なお、注意:このWalkライブラリはGo 1.1でしか実行できない.x以上.
Walkをインストールするのは簡単です.コマンドを実行する限り:
go get github.com/lxn/walk
コマンドの実行が完了するのを待って、GOPATHのsrcとpkgの下に、自分のコンピュータ環境に合ったwalkディレクトリがあるかどうかをチェックします.(私のパソコンのCPUアーキテクチャはAMD 64で、%GOPATH%\windows_amd64\github.com\lxn
の下にwalkフォルダとコンパイル済みのaファイルが現れ、また%GOPATH%\src\github.com\lxn
の下にwalkのソースコードも現れます)また、ツールrsrcをインストールして、後期のパッケージ作業を完了し、コマンドを実行します.
go get github.com/akavel/rsrc
はコマンドの実行が完了するのを待っています.その後、チェックします.ステップは上記と同じで、これ以上は言いません.≪インスタンス|Instance|emdw≫
依存のインポート
import (
// walk
"github.com/lxn/walk"
//declarative , :
."github.com/lxn/walk/declarative"
"strings"
)
func main() {
//
var inTE, outTE *walk.TextEdit
// ,
MainWindow{
//
Title: " Demo",
//
MinSize: Size{600, 400},
// :
Layout: VBox{},
//
Children: []Widget{
// ( )
HSplitter{
//
Children: []Widget{
//
TextEdit{
// inTE
AssignTo: &inTE},
//
TextEdit{
AssignTo: &outTE,
//
ReadOnly: true},
},
},
//
PushButton{
//
Text: " ",
//
OnClicked: func() {
inputStr := inTE.Text()
outputStr := strings.ToUpper(inputStr)
outTE.SetText(outputStr)
},
},
},
}.Run()
}
グラフィックユーザインタフェース2:一般的なコントロールいくつかの一般的なコントロールを認識する
import (
"github.com/lxn/walk"
. "github.com/lxn/walk/declarative"
"os"
"strings"
"io/ioutil"
"fmt"
"log"
)
グローバル・データの定義//
var myAction *walk.Action
//
var myWindow *MyMainWindow
//
type MyMainWindow struct {
*walk.MainWindow
te *walk.TextEdit
//listbox
model *EnvModel
//listbox
listBox *walk.ListBox
}
リストデータモデルの定義//
type EnvItem struct {
//
name string
value string
}
//
type EnvModel struct {
// ListModelBase
walk.ListModelBase
//
items []EnvItem
}
//
func NewEnvModel() *EnvModel {
env := os.Environ()
m := &EnvModel{items: make([]EnvItem, len(env))}
for i, e := range env {
j := strings.Index(e, "=")
if j == 0 {
continue
}
name := e[0:j]
value := strings.Replace(e[j+1:], ";", "\r
", -1)
m.items[i] = EnvItem{name, value}
}
return m
}
リスト関連のリスニングおよびシステムコールバックインタフェースの定義//
func (mw *MyMainWindow) lb_CurrentIndexChanged() {
i := mw.listBox.CurrentIndex()
item := &mw.model.items[i]
mw.te.SetText(item.value)
fmt.Println("CurrentIndex: ", i)
fmt.Println("CurrentEnvVarName: ", item.name)
}
//
func (mw *MyMainWindow) lb_ItemActivated() {
value := mw.model.items[mw.listBox.CurrentIndex()].value
walk.MsgBox(mw, "Value", value, walk.MsgBoxIconInformation)
}
// : listbox
func (m *EnvModel) ItemCount() int {
return len(m.items)
}
// :
func (m *EnvModel) Value(index int) interface{} {
return m.items[index].name
}
通常イベントコールバックの定義//
func ShowMsgBox(title, msg string) int {
return walk.MsgBox(myWindow, " ", " !", walk.MsgBoxOK)
}
//
func TiggerFunc() {
ShowMsgBox(" ", " !")
}
しゅちょう関数func main() {
defer func() {
if err := recover(); err != nil {
errMsg := fmt.Sprintf("%#v", err)
ioutil.WriteFile("fuck.log", []byte(errMsg), 0644)
}
}()
myWindow = &MyMainWindow{model: NewEnvModel()}
if _, err := (MainWindow{
AssignTo: &myWindow.MainWindow,
Title: " ",
//
MenuItems: []MenuItem{
//
Menu{
Text: " ",
//
Items: []MenuItem{
//
Action{
AssignTo: &myAction,
Text: " ",
//
Image: "img/open.png",
//
Shortcut: Shortcut{walk.ModControl, walk.KeyO},
OnTriggered: func() {
ShowMsgBox(" ", " ")
},
},
//
Separator{},
//
Action{
//
Text: " ",
//
OnTriggered: func() {
ShowMsgBox(" ", " ")
},
},
},
},
//
Menu{
Text: " ",
//
Items: []MenuItem{
//
Action{
Text: " ",
//
Image: "img/open.png",
//
Shortcut: Shortcut{walk.ModControl, walk.KeyO},
OnTriggered: func() {
ShowMsgBox(" ", " ")
},
},
//
Separator{},
//
Action{
//
Text: " ",
//
OnTriggered: func() {
ShowMsgBox(" ", " ")
},
},
},
},
},
//
ToolBar: ToolBar{
// :
ButtonStyle: ToolBarButtonImageBeforeText,
//
Items: []MenuItem{
// Action
ActionRef{&myAction},
Separator{},
//
Menu{
//
Text: " 2",
Image: "img/document-properties.png",
OnTriggered: TiggerFunc,
//
Items: []MenuItem{
Action{
Text: "X",
OnTriggered: TiggerFunc,
},
Action{
Text: "Y",
OnTriggered: TiggerFunc,
},
Action{
Text: "Z",
OnTriggered: TiggerFunc,
},
},
},
Separator{},
//
Action{
Text: " 3",
Image: "img/system-shutdown.png",
OnTriggered: func() {
ShowMsgBox(" ", " !")
},
},
},
},
MinSize: Size{600, 400},
Layout: VBox{},
//
Children: []Widget{
//
HSplitter{
MinSize: Size{600, 300},
Children: []Widget{
ListBox{
StretchFactor: 1,
// myWindow.listBox
AssignTo: &myWindow.listBox,
//
Model: myWindow.model,
//
OnCurrentIndexChanged: myWindow.lb_CurrentIndexChanged,
//
OnItemActivated: myWindow.lb_ItemActivated,
},
TextEdit{
StretchFactor: 1,
AssignTo: &myWindow.te,
ReadOnly: true,
},
},
},
HSplitter{
MaxSize:Size{600,50},
Children: []Widget{
//
ImageView{
Background: SolidColorBrush{Color: walk.RGB(255, 191, 0)},
//
Image: "img/open.png",
//
Margin: 5,
//
MinSize: Size{50, 50},
//
Mode: ImageViewModeZoom,
},
//
PushButton{
StretchFactor:8,
Text: " ",
OnClicked: func() {
ShowMsgBox(" ", " !")
},
},
},
},
},
}.Run()); err != nil {
log.Fatal(err)
}
}