go-gtk3开发之grid布局控件
案例说明
创建grid布局控件。
demo.go
package main
import (
"fmt"
"github.com/gotk3/gotk3/glib"
"github.com/gotk3/gotk3/gtk"
"log"
"os"
"reflect"
)
// table.AttachDefaults undefined (type *gtk.TextTagTable has no field or method AttachDefaults)
// Table布局已经取消
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("13_表格布局/builder.ui")
if err != nil {
log.Fatal(err)
}
// 获取window窗口
winObj, _ := builder.GetObject("window1")
window := winObj.(*gtk.Window)
application.AddWindow(window)
// window 窗口设置
window.SetSizeRequest(300, 240) //设置窗口大小
window.SetTitle("hello go") //设置标题
window.SetResizable(false) //设置不可伸缩
window.SetPosition(gtk.WIN_POS_CENTER) //设置居中显示
err = window.SetIconFromFile("images/app.ico") //设置icon
if err != nil {
log.Fatal(err)
}
//获取布局控件
tableObj, err := builder.GetObject("grid1")
if err != nil {
log.Fatal(err)
}
fmt.Println("tableObj", reflect.TypeOf(tableObj))
table := tableObj.(*gtk.Grid)
button, _ := gtk.ButtonNewWithLabel("新按钮") //新建按钮
table.Attach(button, 2, 3, 2, 3)//指定位置添加控件 - 失败
// 显示所有界面
window.ShowAll()
}
builder.ui
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<!-- interface-requires gtk+ 3.0 -->
<object class="GtkWindow" id="window1">
<property name="can_focus">False</property>
<child>
<object class="GtkGrid" id="grid1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<object class="GtkButton" id="button1">
<property name="label" translatable="yes">button</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
<property name="width">1</property>
<property name="height">1</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button2">
<property name="label" translatable="yes">button</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">1</property>
<property name="width">1</property>
<property name="height">1</property>
</packing>
</child>
</object>
</child>
</object>
</interface>