添加行编辑控件,设置行编辑属性(密码不可见,不可编辑,灰色不可编辑),获取内容以及绑定事件,激活行控件后,获取内容。
package main
import (
"fmt"
"github.com/gotk3/gotk3/glib"
"github.com/gotk3/gotk3/gtk"
"log"
"os"
"reflect"
)
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("10_行编辑/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)
}
//获取entry控件
entryObj1, _ := builder.GetObject("entry1")
entryObj2, _ := builder.GetObject("entry2")
entryObj3, _ := builder.GetObject("entry3")
entry1 := entryObj1.(*gtk.Entry)
entry2 := entryObj2.(*gtk.Entry)
entry3 := entryObj3.(*gtk.Entry)
fmt.Println("hboxObj", reflect.TypeOf(entryObj3))
entry1.SetText("123456")
//设置内容
entry1Text, _ := entry1.GetText()
fmt.Println("entry1 text = ", entry1Text) //获取内容
//entry1.ModifyFontSize(30) //修改字体大小 - 失败
entry2.SetEditable(false) //只读,不可编辑
entry2.SetSensitive(false) //变灰色,不能编辑
entry3.SetVisibility(false) //设置不可见字符,即密码模式
//信号处理,当用户在文本输入控件内部按回车键时引发activate信号
_, _ = entry1.Connect("activate", func() {
entry1Text, _ := entry1.GetText()
fmt.Println("entry text = ", entry1Text) //获取内容
})
// 显示所有界面
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="GtkImage" id="image1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="stock">gtk-missing-image</property>
</object>
<object class="GtkWindow" id="window1">
<property name="can_focus">False</property>
<child>
<object class="GtkFixed" id="fixed1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkEntry" id="entry1">
<property name="width_request">100</property>
<property name="height_request">80</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="invisible_char">●</property>
<property name="primary_icon_activatable">False</property>
<property name="secondary_icon_activatable">False</property>
<property name="primary_icon_sensitive">True</property>
<property name="secondary_icon_sensitive">True</property>
</object>
<packing>
<property name="x">18</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="entry2">
<property name="width_request">100</property>
<property name="height_request">80</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="invisible_char">●</property>
<property name="primary_icon_activatable">False</property>
<property name="secondary_icon_activatable">False</property>
<property name="primary_icon_sensitive">True</property>
<property name="secondary_icon_sensitive">True</property>
</object>
<packing>
<property name="x">17</property>
<property name="y">86</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="entry3">
<property name="width_request">100</property>
<property name="height_request">80</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="invisible_char">●</property>
<property name="primary_icon_activatable">False</property>
<property name="secondary_icon_activatable">False</property>
<property name="primary_icon_sensitive">True</property>
<property name="secondary_icon_sensitive">True</property>
</object>
<packing>
<property name="x">17</property>
<property name="y">183</property>
</packing>
</child>
</object>
</child>
</object>
</interface>