纯Go语言WebUI工具包:Gowut
jopen
12年前
gowut是一个 功能全面、简单易用、平台无关的纯go语言WebUI工具包。
例子如下:
package main import ( "code.google.com/p/gowut/gwu" "strconv" ) type MyButtonHandler struct { counter int text string } func (h *MyButtonHandler) HandleEvent(e gwu.Event) { if b, isButton := e.Src().(gwu.Button); isButton { b.SetText(b.Text() + h.text) h.counter++ b.SetToolTip("You've clicked " + strconv.Itoa(h.counter) + " times!") e.MarkDirty(b) } } func main() { // Create and build a window win := gwu.NewWindow("main", "Test GUI Window") win.Style().SetFullWidth() win.SetHAlign(gwu.HA_CENTER) win.SetCellPadding(2) // Button which changes window content win.Add(gwu.NewLabel("I'm a label! Try clicking on the button=>")) btn := gwu.NewButton("Click me") btn.AddEHandler(&MyButtonHandler{text: ":-)"}, gwu.ETYPE_CLICK) win.Add(btn) btnsPanel := gwu.NewNaturalPanel() btn.AddEHandlerFunc(func(e gwu.Event) { // Create and add a new button... newbtn := gwu.NewButton("Extra #" + strconv.Itoa(btnsPanel.CompsCount())) newbtn.AddEHandlerFunc(func(e gwu.Event) { btnsPanel.Remove(newbtn) // ...which removes itself when clicked e.MarkDirty(btnsPanel) }, gwu.ETYPE_CLICK) btnsPanel.Insert(newbtn, 0) e.MarkDirty(btnsPanel) }, gwu.ETYPE_CLICK) win.Add(btnsPanel) // ListBox examples p := gwu.NewHorizontalPanel() p.Style().SetBorder2(1, gwu.BRD_STYLE_SOLID, gwu.CLR_BLACK) p.SetCellPadding(2) p.Add(gwu.NewLabel("A drop-down list being")) widelb := gwu.NewListBox([]string{"50", "100", "150", "200", "250"}) widelb.Style().SetWidth("50") widelb.AddEHandlerFunc(func(e gwu.Event) { widelb.Style().SetWidth(widelb.SelectedValue() + "px") e.MarkDirty(widelb) }, gwu.ETYPE_CHANGE) p.Add(widelb) p.Add(gwu.NewLabel("pixel wide. And a multi-select list:")) listBox := gwu.NewListBox([]string{"First", "Second", "Third", "Forth", "Fifth", "Sixth"}) listBox.SetMulti(true) listBox.SetRows(4) p.Add(listBox) countLabel := gwu.NewLabel("Selected count: 0") listBox.AddEHandlerFunc(func(e gwu.Event) { countLabel.SetText("Selected count: " + strconv.Itoa(len(listBox.SelectedIndices()))) e.MarkDirty(countLabel) }, gwu.ETYPE_CHANGE) p.Add(countLabel) win.Add(p) // Self-color changer check box greencb := gwu.NewCheckBox("I'm a check box. When checked, I'm green!") greencb.AddEHandlerFunc(func(e gwu.Event) { if greencb.State() { greencb.Style().SetBackground(gwu.CLR_GREEN) } else { greencb.Style().SetBackground("") } e.MarkDirty(greencb) }, gwu.ETYPE_CLICK) win.Add(greencb) // TextBox with echo p = gwu.NewHorizontalPanel() p.Add(gwu.NewLabel("Enter your name:")) tb := gwu.NewTextBox("") tb.AddSyncOnETypes(gwu.ETYPE_KEY_UP) p.Add(tb) p.Add(gwu.NewLabel("You entered:")) nameLabel := gwu.NewLabel("") nameLabel.Style().SetColor(gwu.CLR_RED) tb.AddEHandlerFunc(func(e gwu.Event) { nameLabel.SetText(tb.Text()) e.MarkDirty(nameLabel) }, gwu.ETYPE_CHANGE, gwu.ETYPE_KEY_UP) p.Add(nameLabel) win.Add(p) // Create and start a GUI server (omitting error check) server := gwu.NewServer("guitest", "localhost:8081") server.SetText("Test GUI App") server.AddWin(win) server.Start("") // Also opens windows list in browser }
结果如下: