创建按钮,绑定事件,点击按钮后弹出对话框。
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” 释放按钮时触发
*/
<?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>