Golang Gtk+3教程:Grid布局

在上个例子中我们使用了box布局,现在让我们来学习另一种布局——grid。其实这几种布局都大同小异,如果你看懂了上一个例子,想必使用grid也不是难事。

程序运行效果:

gtk3-grid.png

package main

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

func main() {
    const appId = "com.nayoso.example"

    app, _ := gtk.ApplicationNew(appId, glib.APPLICATION_FLAGS_NONE)
    app.Connect("activate", func() {
        onActivate(app)
    })
    app.Run(os.Args)
}

func onActivate(application *gtk.Application) {
    appWindow, _ := gtk.ApplicationWindowNew(application)
    appWindow.SetTitle("Grid example")
    //-- 以上,通常的代码输入完了,接下就是这个例子的重点了:-D

    grid, _ := gtk.GridNew()    //创建容器
    appWindow.Add(grid)     //将容器添加到window中

    //现在再让我们创建一些按钮来展示grid的效果
    button1, _ := gtk.ButtonNewWithLabel("Button 1")
    button2, _ := gtk.ButtonNewWithLabel("Button 2")
    button3, _ := gtk.ButtonNewWithLabel("Button 3")
    //将buttons添加到grid中
    grid.Attach(button1, 0, 0, 1, 1)    //参数:左,上,宽,高
    grid.Attach(button2, 1, 0, 1, 1)
    grid.Attach(button3, 0, 1, 2, 1)
    //-- 注意一下,按钮的位置就像在一个坐标轴中,原点在左上,x轴向右,y轴向下
    //-- 如果你不是很喜欢或者很懂这种方式也没关系,后面我还会介绍可视化的UI设计工具

    appWindow.ShowAll()
}

你可能发现了,我有时会使用容器和布局来称呼同一个东西。实际上,这是因为其同时具有这两种性质。


知识共享许可协议
本作品采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议进行许可。转载请注明出处!

转载于:https://www.cnblogs.com/xiyu714/p/9897408.html

  • 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、付费专栏及课程。

余额充值