go-gtk3开发之Grid布局控件(15)

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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值