go-gtk3开发之对话框控件(16)

go-gtk3开发之对话框控件

案例说明

创建按钮,绑定事件,点击按钮后弹出对话框。

demo.go

package main

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


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("14_对话框/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)
	}
	buttonObj1, _ := builder.GetObject("button1")
	buttonObj2, _ := builder.GetObject("button2")
	button1 := buttonObj1.(*gtk.Button)
	button2 := buttonObj2.(*gtk.Button)

	//信号处理
	_, _ = button1.Connect("clicked", func() {
		//新建消息对话框,选择对话框
		dialog := gtk.MessageDialogNew(
			window, //指定父窗口
			gtk.DIALOG_MODAL,              //模态对话框
			gtk.MESSAGE_QUESTION,          //指定对话框类型
			gtk.BUTTONS_YES_NO,            //默认按钮
			"Are u ok?") //设置内容

		dialog.SetTitle("问题对话框") //对话框设置标题

		flag := dialog.Run() //运行对话框
		if flag == gtk.RESPONSE_YES {
			fmt.Println("按下yes")
		} else if flag == gtk.RESPONSE_NO {
			fmt.Println("按下no")
		} else {
			fmt.Println("按下关闭按钮")
		}

		dialog.Destroy() //销毁对话框
	})

	_, _ = button2.Connect("clicked", func() {
		dialog := gtk.MessageDialogNew(
			window,           //指定父窗口
			gtk.DIALOG_MODAL, //模态对话框
			gtk.MESSAGE_INFO, //info类型
			gtk.BUTTONS_OK,   //默认按钮
			"结束了") //设置内容

		dialog.Run()     //运行对话框
		dialog.Destroy() //销毁对话框
	})

	// 显示所有界面
	window.ShowAll()
}

/*
信号标识	触发条件
“clicked”	按下按钮时触发
“pressed”	按下按钮时触发
“released”	释放按钮时触发
*/

builder.ui

<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <requires lib="gtk+" version="2.24"/>
  <!-- interface-naming-policy project-wide -->
  <object class="GtkWindow" id="window1">
    <property name="can_focus">False</property>
    <child>
      <object class="GtkHBox" id="hbox1">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <child>
          <object class="GtkButton" id="button1">
            <property name="label" translatable="yes">问题对话框</property>
            <property name="use_action_appearance">False</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
          </object>
          <packing>
            <property name="expand">True</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkButton" id="button2">
            <property name="label" translatable="yes">消息对话框</property>
            <property name="use_action_appearance">False</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
          </object>
          <packing>
            <property name="expand">True</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

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

余额充值