go-gtk3开发之水平布局控件(13)

go-gtk3开发之水平布局控件

案例说明

创建水平布局,新版不再支持GtkHBox/GtkVBox布局。

demo.go

package main

import (
	"fmt"
	"github.com/gotk3/gotk3/glib"
	"github.com/gotk3/gotk3/gtk"
	"log"
	"os"
	"reflect"
)

// unrecognized class name 'GtkHBox'
// 新版不再支持GtkHBox/GtkVBox
// glade3 3.8 编辑器太老了, 使用glade-3.14

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("11_水平布局/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)
	}

	//获取hbox控件
	hboxObj, err := builder.GetObject("box2")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("hboxObj", reflect.TypeOf(hboxObj))

	hbox := hboxObj.(*gtk.Box)
	//gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 3)
	button, _ := gtk.ButtonNewWithLabel("新按钮") //新建按钮
	hbox.Add(button)                           //按钮添加到布局中


	// 显示所有界面
	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="GtkBox" id="box2">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="homogeneous">True</property>
        <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="expand">False</property>
            <property name="fill">True</property>
            <property name="position">0</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="expand">False</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
        <child>
          <placeholder/>
        </child>
      </object>
    </child>
  </object>
</interface>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
gotk3 提供 Go 绑定 GTK 3 和依赖的其他项目。每个组件都给出了用来导入包路径的子目录。以下是部分已经实施的支持库:GTK 3 (3.6 and later)GDK 3 (3.6 and later)GLib 2 (2.36 and later)Cairo (1.10 and later)已经采取谨慎的内存管理与Go的垃圾收集器无缝工作,而无需使用或理解图形对象的浮动参考。简单示例:package main import (     "github.com/conformal/gotk3/gtk"     "log" ) func main() {     // Initialize GTK without parsing any command line arguments.     gtk.Init(nil)     // Create a new toplevel window, set its title, and connect it to the     // "destroy" signal to exit the GTK main loop when it is destroyed.     win, err := gtk.WindowNew(gtk.WINDOW_TOPLEVEL)     if err != nil {         log.Fatal("Unable to create window:", err)     }     win.SetTitle("Simple Example")     win.Connect("destroy", func() {         gtk.MainQuit()     })     // Create a new label widget to show in the window.     l, err := gtk.LabelNew("Hello, gotk3!")     if err != nil {         log.Fatal("Unable to create label:", err)     }     // Add the label to the window.     win.Add(l)     // Set the default window size.     win.SetDefaultSize(800, 600)     // Recursively show all widgets contained in this window.     win.ShowAll()     // Begin executing the GTK main loop.  This blocks until     // gtk.MainQuit() is run.      gtk.Main() } 标签:gotk3

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值