目录
Github
git clone https://github.com/gotk3/gotk3.git
简介
- gotk3 项目为 GTK 3 和依赖项目提供 Go 绑定。每个组件都有自己的子目录,用作包的导入路径。目前已实现对以下库的部分绑定支持:
- GTK 3(3.12 及更高版本)
- GDK 3(3.12 及更高版本)
- GLib 2(2.36 及更高版本)
环境变量
vim ~/.bash_profile
# 添加 PKG_CONFIG_PATH
export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig/:`brew --prefix gtk+3`/lib/pkgconfig go get -u -v github.com/gotk3/gotk3/gdk:$PKG_CONFIG_PATH"
source ~/.bash_profile
官方示例
git clone https://github.com/gotk3/gotk3-examples.git
# 没有go.mod文件则需要初始化
go mod init example.com/glade
go mod tidy
# gotk3依赖
# go get github.com/gotk3/gotk3/gtk
go run glade.go
demo.glade
Github
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.40.0 -->
<interface>
<requires lib="gtk+" version="3.24"/>
<object class="GtkWindow" id="window">
<property name="width-request">400</property>
<property name="height-request">200</property>
<property name="can-focus">False</property>
<property name="title" translatable="yes">demo</property>
<child>
<object class="GtkBox" id="box">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkButton" id="button">
<property name="label" translatable="yes">button</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="margin-start">10</property>
<property name="margin-end">10</property>
<property name="margin-top">10</property>
<property name="margin-bottom">10</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkComboBoxText" id="combobox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="margin-start">10</property>
<property name="margin-end">10</property>
<property name="margin-top">10</property>
<property name="margin-bottom">10</property>
<property name="active">0</property>
<property name="active-id">1</property>
<items>
<item id="1" translatable="yes">item1</item>
<item id="2" translatable="yes">item2</item>
<item id="3" translatable="yes">item3</item>
<item id="4" translatable="yes">item4</item>
</items>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="entry">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="margin-start">10</property>
<property name="margin-end">10</property>
<property name="margin-top">10</property>
<property name="margin-bottom">10</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
main.go
package main
import (
"github.com/gotk3/gotk3/gtk"
"log"
)
func main() {
gtk.Init(nil)
builder, err := gtk.BuilderNewFromFile("ui/demo.glade")
if err != nil {
log.Fatal("Error loading Glade file:", err)
}
buildButton(builder)
buildCombobox(builder)
buildEntry(builder)
window := buildWindow(builder)
window.ShowAll()
gtk.Main()
}
func buildWindow(builder *gtk.Builder) *gtk.Window {
obj, err := builder.GetObject("window")
if err != nil {
log.Fatal("Error getting window object:", err)
}
window, ok := obj.(*gtk.Window)
if !ok {
log.Fatal("Error converting to gtk.Window")
}
window.Connect("destroy", func() {
log.Println("Close Window")
gtk.MainQuit()
})
return window
}
func buildButton(builder *gtk.Builder) {
obj, err := builder.GetObject("button")
if err != nil {
log.Fatal("Error getting Button object:", err)
}
button, ok := obj.(*gtk.Button)
if !ok {
log.Fatal("Error converting to gtk.Button")
}
button.Connect("clicked", func() {
log.Println("Hello World")
})
}
func buildCombobox(builder *gtk.Builder) {
obj, err := builder.GetObject("combobox")
if err != nil {
log.Fatal("Error getting ComboBoxText object:", err)
}
combobox, ok := obj.(*gtk.ComboBoxText)
if !ok {
log.Fatal("Error converting to gtk.ComboBoxText")
}
combobox.Connect("changed", func() {
activeIndex := combobox.GetActive()
activeText := combobox.GetActiveText()
log.Println("ComboBoxText index item changed:", activeIndex, activeText)
})
}
func buildEntry(builder *gtk.Builder) {
obj, err := builder.GetObject("entry")
if err != nil {
log.Fatal("Error getting entry object:", err)
}
entry, ok := obj.(*gtk.Entry)
if !ok {
log.Fatal("Error converting to gtk.Entry")
}
entry.Connect("changed", func() {
text, err := entry.GetText()
if err != nil {
log.Fatal("Error getting text from entry:", err)
}
log.Println("Entry text changed:", text)
})
entry.Connect("activate", func() {
text, err := entry.GetText()
if err != nil {
log.Fatal("Error getting text from entry:", err)
}
log.Println("Entry text activated:", text)
})
}
标签:10,log,err,builder,gtk,Glade,gotk3,GO,GOTK3
From: https://www.cnblogs.com/wufengsheng/p/18002875