go-gui- control and signal

go-gui- control and signal

Controls

Controls Introduction

Control is encapsulation of data and methods. Control has its own attributes and methods. Attribute refers to the characteristics of the control. Method refers to some simple and visible function control. Such as a button is a control, this button is square, there are pictures, which we can see the appearance of the property, at the same time, this button has been pressed people function.

GTK the controls are mainly divided into two categories: container control, non-container control.

Container control: it can hold other controls, we can be understood as a box, the box used to hold things. Container control is divided into two categories, one can receive a control, such as windows, buttons; another to accommodate a plurality of controls, such as control layout.
Non-container control: it can not accommodate other controls, such as labels, line editing.

package main

import (
    "os"

    "github.com/mattn/go-gtk/gtk"
)

func main() {
    gtk.Init(&os.Args) //环境初始化

    //--------------------------------------------------------
    // 主窗口
    //--------------------------------------------------------
    window := gtk.NewWindow(gtk.WINDOW_TOPLEVEL) //创建窗口
    window.SetPosition(gtk.WIN_POS_CENTER)       //设置窗口居中显示
    window.SetTitle("GTK Go!")                   //设置标题
    window.SetSizeRequest(300, 200)              //设置窗口的宽度和高度

    //--------------------------------------------------------
    // GtkFixed
    //--------------------------------------------------------
    layout := gtk.NewFixed() //创建固定布局

    //--------------------------------------------------------
    // GtkButton
    //--------------------------------------------------------
    b1 := gtk.NewButton() //新建按钮
    b1.SetLabel("^_@")    //设置内容
    //b1.SetSizeRequest(100, 50) //设置按钮大小

    b2 := gtk.NewButtonWithLabel("@_~") //新建按钮,同时设置内容
    b2.SetSizeRequest(100, 50)          //设置按钮大小

    //--------------------------------------------------------
    // 添加布局、添加容器
    //--------------------------------------------------------
    window.Add(layout) //把布局添加到主窗口中

    layout.Put(b1, 0, 0)    //设置按钮在容器的位置
    layout.Move(b1, 50, 50) //移动按钮的位置,必须先put,再用move

    layout.Put(b2, 50, 100)

    window.ShowAll() //显示所有的控件

    gtk.Main() //主事件循环,等待用户操作
}

//func (v *Fixed) Put(w IWidget, x, y int)
//功能:固定布局容器添加控件
//参数:
//    widget:要添加的控件
//    x, y:控件摆放位置的起点坐标

signal

GTK using a callback function to process the signals coming from outside the window event messages or signals. When the signal occurs, the signal is automatically invoked connected (registered) callback function.

Learn GUI programming, we often come into contact "signal" of the term. GTK "Signal" is actually a software interrupt. "Interrupt" often encountered in our lives, for example, I was playing a game room, suddenly express delivery came and took me to the game being played, "break", I went to sign for delivery (interrupt handling), processing is completed after, continue to play my game. GTK "Signal" is to belong to such a kind of "break", when the user presses the button, it creates a "break", equivalent to generate a signal, then such a deal would "interrupt task" (program in Experience to call a function).

"Signal" in GTK can be considered a sign of disruption, such as pressing a button marked as "pressed", mark the release button is "released", these flags equivalent to language keywords go, like, when we're using We must be fully in accordance with its name to write. It should be noted that each control signal sign is not necessarily the same, such as buttons (GtkButton), there are "pressed" signal window (GtkWindow) in the no signal, each of which has specific control signal, should help documentation to make sure.
The trigger signal conditions

"Clicked" trigger button is pressed
"pressed" trigger button is pressed
triggered when "released" release button

package main

import (
    "fmt"
    "os"

    "github.com/mattn/go-gtk/glib"
    "github.com/mattn/go-gtk/gtk"
)

//按钮b1信号处理的回调函数
func HandleButton(ctx *glib.CallbackContext) {
    arg := ctx.Data()   //获取用户传递的参数,是空接口类型
    p, ok := arg.(*int) //类型断言
    if ok {             //如果ok为true,说明类型断言正确
        fmt.Println("*p = ", *p) //用户传递传递的参数为&tmp,是一个变量的地址
        *p = 250                 //操作指针所指向的内存
    }

    fmt.Println("按钮b1被按下")

    //gtk.MainQuit() //关闭gtk程序
}

func main() {
    gtk.Init(&os.Args) //环境初始化

    //--------------------------------------------------------
    // 主窗口
    //--------------------------------------------------------
    window := gtk.NewWindow(gtk.WINDOW_TOPLEVEL) //创建窗口
    window.SetPosition(gtk.WIN_POS_CENTER)       //设置窗口居中显示
    window.SetTitle("GTK Go!")                   //设置标题
    window.SetSizeRequest(300, 200)              //设置窗口的宽度和高度

    //--------------------------------------------------------
    // GtkFixed
    //--------------------------------------------------------
    layout := gtk.NewFixed() //创建固定布局

    //--------------------------------------------------------
    // GtkButton
    //--------------------------------------------------------
    b1 := gtk.NewButton() //新建按钮
    b1.SetLabel("^_@")    //设置内容
    //b1.SetSizeRequest(100, 50) //设置按钮大小

    b2 := gtk.NewButtonWithLabel("@_~") //新建按钮,同时设置内容
    b2.SetSizeRequest(100, 50)          //设置按钮大小

    //--------------------------------------------------------
    // 添加布局、添加容器
    //--------------------------------------------------------
    window.Add(layout) //把布局添加到主窗口中

    layout.Put(b1, 0, 0)    //设置按钮在容器的位置
    layout.Move(b1, 50, 50) //移动按钮的位置,必须先put,再用move

    layout.Put(b2, 50, 100)

    //--------------------------------------------------------
    // 信号处理
    //--------------------------------------------------------
    //按钮按下自动触发"pressed",自动调用HandleButton, 同时将 &tmp 传递给HandleButton
    tmp := 10
    b1.Connect("pressed", HandleButton, &tmp)

    //回调函数为匿名函数,推荐写法
    //按钮按下自动触发"pressed",自动调用匿名函数,
    b2.Connect("pressed", func() {

        fmt.Println("b2被按下")
        fmt.Println("tmp = ", tmp)

    }) //注意:}和)在同一行

    window.ShowAll() //显示所有的控件

    gtk.Main() //主事件循环,等待用户操作
}

//func (v *Widget) Connect(s string, f interface{}, datas ...interface{}) int
//功能:信号注册
//参数:
//    v: 信号发出者,可以认为我们操作的控件,如按下按钮,这个就为按钮指针
//    s:信号标志,如"pressed"
//    f:回调函数的名称,
//    datas:给回调函数传的参数,尽管是可变参数,但是只能传递一个参数,可变参数的目的为了让用户多个选择(可以传参,或者不传)
//返回值:
//    注册函数的标志

Guess you like

Origin www.cnblogs.com/ygjzs/p/11946164.html