使用builder进行开发一个界面。
package main
import (
"github.com/gotk3/gotk3/glib"
"github.com/gotk3/gotk3/gtk"
"log"
"os"
)
func main() {
const appId = "com.nayoso.example"
app, _ := gtk.ApplicationNew(appId, glib.APPLICATION_FLAGS_NONE)
_, err := app.Connect("activate", func() {
createWindow(app)
})
if err != nil {
log.Fatal(err)
}
app.Run(os.Args)
}
func createWindow(application *gtk.Application) {
// 从文件中创建Builder
builder, err := gtk.BuilderNewFromFile("5_窗口/builder.ui")
if err != nil {
log.Fatal(err)
}
// 从文件中读取window对象,其实际上是Gobject
winObj, err := builder.GetObject("window1")
if err != nil {
log.Fatal(err)
}
//由于winObj是Gobject,所以我们使用类型断言得到Gtk.Window对象
window := winObj.(*gtk.Window)
//记得将window加入我们的application中
application.AddWindow(window)
window.SetSizeRequest(300, 240) //设置窗口大小
window.SetTitle("hello go") //设置标题
err = window.SetIconFromFile("images/app.ico") //设置icon
if err != nil {
log.Fatal(err)
}
window.SetResizable(false) //设置不可伸缩
window.SetPosition(gtk.WIN_POS_CENTER) //设置居中显示
// 显示所有界面
window.ShowAll()
}
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk+" version="2.24"/>
<!-- interface-naming-policy project-wide -->
<object class="GtkWindow" id="window1">
<property name="can_focus">False</property>
<child>
<placeholder/>
</child>
</object>
</interface>