<> Five pictures to understand golang GUI programming

Code arrangement
package main import "os" //import "os/exec" //import "fmt" import
"github.com/mattn/go-gtk/gtk" import "github.com/mattn/go-gtk/glib" func
Handl(ctx *glib.CallbackContext) { //fmt.Println("hhhhhhhh") } func main() {
// initialization gtk.Init(&os.Args) builder := gtk.NewBuilder()
builder.AddFromFile("./test.glade") win :=
gtk.WindowFromObject(builder.GetObject("window")) win.Show() // User written code win :=
gtk.NewWindow(gtk.WINDOW_TOPLEVEL) win.SetTitle("GO GTK")
win.SetSizeRequest(420, 380) layout := gtk.NewFixed() win.Add(layout)
win.SetIconFromFile("./ Parallel concurrency .png") b := gtk.NewButtonWithLabel("*_*")
b.SetSizeRequest(100, 100) layout.Put(b, 0, 100) b.Connect("clicked", Handl,
"gggg") win.ShowAll() win.SetDefaultSize(100, 100) win.Connect("destroy",
func() { //fmt.Println("+++++") gtk.MainQuit() }) // Main event cycle gtk.Main()
//fmt.Println(" It's closed ") } package main import ( "fmt" "os"
"github.com/mattn/go-gtk/glib" "github.com/mattn/go-gtk/gtk" ) func main() {
//1. initialization gtk.Init(&os.Args) // Fixed initialization preparation environment //2. User written code //a. create a window //b. Set window properties ( title , size )
//c. Display window win := gtk.NewWindow(gtk.WINDOW_TOPLEVEL) // Top level window with border win.SetTitle("go
gtk") // Set title win.SetSizeRequest(480, 320) // Set window size // display // Create container control ( Fixed layout , Arbitrary layout )
// Add layout to window // Create button // Button to add to layout // Display controls layout := gtk.NewFixed() win.Add(layout)
// Add layout to window b1 := gtk.NewButtonWithLabel("^_^") b2 :=
gtk.NewButtonWithLabel("@_@") b2.SetSizeRequest(100, 100) // Set button 2 size
layout.Put(b1, 0, 0) //0,0 coordinate layout.Put(b2, 50, 50) //win.Show()
//layout.Show() //b1.Show() //b2.Show() win.ShowAll() // Multiple controls , Controls created with code require showall
// signal processing // Button press trigger signal :clicked str := "are you ok?" b1.Connect("clicked",
HandleSignal, str) // trouble , The handler can be anonymous , Recommended writing /*b2.Connect("clicked", func() {
fmt.Println("222222222222") })*/ b2.Clicked(func() { //Clicked It's packaged , More concise
fmt.Println("222222222222") }) // Press the close button of the window , trigger "destroy" win.Connect("destroy",
func() { fmt.Println(" The window is closed ") gtk.MainQuit() //gtk Program close })
// Tell the system , Just press the button , Auto call HandleSignal,str It's for you HandleSignal Function of
//Connect() It will only be called once , Tell the system a rule , Just tell it once //3. Main event cycle ( fixed ) //a Let the program not end ,b
Waiting for user action ( move windows , Click the mouse ) //1.3. Basically fixed gtk.Main() // block fmt.Println("over") } func
HandleSignal(ctx *glib.CallbackContext) { // Pay attention to parameter writing , //fmt.Println(ctx.Data())
// Get the data passed by the user , Empty interface type arg := ctx.Data() data, ok := arg.(string) // Type Asserts , Reverses the type of raw data
if ok { fmt.Printf(" The button is pressed ,%s\n", data) } } package main import ( "fmt" "os"
"strconv" "time" "github.com/mattn/go-gtk/gdk"
"github.com/mattn/go-gtk/gdkpixbuf" "github.com/mattn/go-gtk/gtk" ) func main()
{ gtk.Init(&os.Args) // load glade file builder := gtk.NewBuilder()
builder.AddFromFile("C:/Users/dawnl/Desktop/GO/src/GTK/test1.glade")
// obtain glade Controls for win := gtk.WindowFromObject(builder.GetObject("window1"))
//win.Show() // Display controls , If it is passed glade Added control ,show() that will do // If the control is added through the code layout , One by one show or showall
b1 := gtk.ButtonFromObject(builder.GetObject("button1")) b2 :=
gtk.ButtonFromObject(builder.GetObject("button2")) label :=
gtk.LabelFromObject(builder.GetObject("label1")) label.SetText(" Your uncle ")
label.ModifyFontSize(30) label.ModifyFG(gtk.STATE_NORMAL,
gdk.NewColor("BLACK")) image :=
gtk.ImageFromObject(builder.GetObject("image1")) var x, y int
image.GetSizeRequest(&x, &y) pixbuf, _ :=
gdkpixbuf.NewPixbufFromFileAtScale("C:/Users/dawnl/Desktop/web.png", x-10,
y-10, false) image.SetFromPixbuf(pixbuf) pixbuf.Unref() // Get progress bar control pg :=
gtk.ProgressBarFromObject(builder.GetObject("progressbar1")) ch := make(chan
int) go func() { t := 1.0 for i := 1; i <= 1000; i++ { pg.SetFraction(t)
pg.SetText(strconv.Itoa(100-i/10) + "%") time.Sleep(time.Millisecond * 10) t -=
0.001 if i == 1000 { ch <- 1 fmt.Println(" end ") } } }() go func() {
fmt.Println(" block ") <-ch //win.Destroy() }() win.Show() // signal processing b1.Clicked( func()
{ fmt.Println(" It's pressed button1") }) b2.Clicked( func() { fmt.Println(" It's pressed button2")
}) win.Connect("destroy", func() { gtk.MainQuit() }) gtk.Main() } // Keyboard and mouse events
package main import "fmt" import "os" import "github.com/mattn/go-gtk/gtk"
import "github.com/mattn/go-gtk/gdk" import "github.com/mattn/go-gtk/glib"
import "unsafe" func main() { gtk.Init(&os.Args) win :=
gtk.NewWindow(gtk.WINDOW_TOPLEVEL) win.SetSizeRequest(420, 380)
//gdk.BUTTON_PRESS_MASK Mouse down , Trigger signal ,button-press-event //gdk.BUTTON1_MOTION_MASK
Mouse movement , Press any key to move Trigger signal motion-notify-event
win.SetEvents(int(gdk.BUTTON_PRESS_MASK | gdk.BUTTON1_MOTION_MASK)) // Set capture properties //
Start when mouse down ,"button-press-event" win.Connect("button-press-event", func(ctx
*glib.CallbackContext) { //fmt.Println(" Mouse down ") arg := ctx.Args(0) event :=
*(**gdk.EventButton)(unsafe.Pointer(&arg))
//fmt.Println(event.Button)// Left key 1,2 Middle key ,3 Right click flag := event.Button if flag == 1 {
fmt.Print(" Left key \t") } else if flag == 2 { fmt.Print(" Middle key \t") } else {
fmt.Print(" Right click \t") } if event.Type == int(gdk.BUTTON_PRESS) { fmt.Print(" single click \t")
} else if event.Type == int(gdk.BUTTON2_PRESS) { fmt.Print(" double-click \t") }
fmt.Printf(" Relative to window :( %v,%v )\t", event.X, event.Y) fmt.Printf(" Relative to the screen :( %v,%v
)\n", event.XRoot, event.YRoot) }) // Trigger when changing window size win.Connect("configure-event",
func() { var w, h int win.GetSize(&w, &h) fmt.Printf("w=%d h=%d\n", w, h)
// For the first time, Windows start from scratch // Change the window for the second time }) win.Connect("key-press-event", func(ctx
*glib.CallbackContext) { // Gets the keyboard press property structure variable , System internal variables , It is not a variable passed by the user arg := ctx.Args(0)
event := *(**gdk.EventKey)(unsafe.Pointer(&arg)) // What we get is actually the number of letters ascii code key :=
event.Keyval fmt.Printf("key===%v\n", key) if key == gdk.KEY_a {
fmt.Println("aaaaaaa") } }) win.Connect("motion-notify-event", func(ctx
*glib.CallbackContext) { // Gets the mouse down attribute structure variable , System internal variables , It is not a variable passed by the user arg := ctx.Args(0)
event := *(**gdk.EventButton)(unsafe.Pointer(&arg)) fmt.Printf("====== Relative to window :(
%v,%v ) ", event.X, event.Y) fmt.Printf("====== Relative to the screen :( %v,%v )\n",
event.XRoot, event.YRoot) }) win.Show() win.Connect("destroy", func() {
gtk.MainQuit() }) gtk.Main() } // Drawing events package main //import "fmt" import "os"
import "github.com/mattn/go-gtk/gtk" import "github.com/mattn/go-gtk/gdk"
import "github.com/mattn/go-gtk/gdkpixbuf" func main() { gtk.Init(&os.Args)
builder := gtk.NewBuilder() builder.AddFromFile("./paint.glade") win :=
gtk.WindowFromObject(builder.GetObject("window1")) win.Connect("destroy",
func() { gtk.MainQuit() }) var w, h int win.Connect("configure-event", func() {
win.GetSize(&w, &h) //fmt.Println("w:", w, " h:", h) // Drawing , The whole window area is painted
win.QueueDraw() // Trigger the next one expose-event signal }) // Draw the background picture , Allow window drawing
win.SetAppPaintable(true) x := 0 win.Connect("expose-event", func() {
// Set the painter to specify the drawing area painter := win.GetWindow().GetDrawable() gc := gdk.NewGC(painter)
// Create image resources bg, _ :=
gdkpixbuf.NewPixbufFromFileAtScale("C:/Users/dawnl/Desktop/ Material woman .jpg", w, h,
false) face, _ :=
gdkpixbuf.NewPixbufFromFileAtScale("C:/Users/dawnl/Desktop/ funny .jpg", 85, 80,
false) painter.DrawPixbuf(gc, bg, 0, 0, 0, 0, -1, -1, gdk.RGB_DITHER_NONE, 0,
0) painter.DrawPixbuf(gc, face, 0, 0, x, 150, -1, -1, gdk.RGB_DITHER_NONE, 0,
0) bg.Unref() // Release image resources face.Unref() }) // Get button butt :=
gtk.ButtonFromObject(builder.GetObject("button1")) butt.Clicked(func() { x +=
50 if x >= w { x = 0 } win.QueueDraw() // Trigger the next one expose-event signal }) win.Show()
gtk.Main() } // Borderless window move package main import "os" import
"github.com/mattn/go-gtk/gtk" import "unsafe" import
"github.com/mattn/go-gtk/gdk" import "github.com/mattn/go-gtk/glib" func main()
{ gtk.Init(&os.Args) win := gtk.NewWindow(gtk.WINDOW_TOPLEVEL)
win.SetSizeRequest(480, 320) win.SetDecorated(false) // Debounding x, y := 0, 0
win.SetEvents(int(gdk.BUTTON_PRESS_MASK | gdk.BUTTON1_MOTION_MASK)) // Set capture properties //
Start when mouse down ,"button-press-event" win.Connect("button-press-event", func(ctx
*glib.CallbackContext) { //fmt.Println(" Mouse down ") arg := ctx.Args(0) event :=
*(**gdk.EventButton)(unsafe.Pointer(&arg)) x, y = int(event.X), int(event.Y) if
event.Button == 3 { //3 It's the right mouse button gtk.MainQuit() } })
win.Connect("motion-notify-event", func(ctx *glib.CallbackContext) {
// Gets the mouse down attribute structure variable , System internal variables , It is not a variable passed by the user arg := ctx.Args(0) event :=
*(**gdk.EventButton)(unsafe.Pointer(&arg)) win.Move(int(event.XRoot)-x,
int(event.YRoot)-y) }) win.Show() gtk.Main() }
Just write about it
GUI: Graphical interface , It is for the convenience of non professional users

character based interface , To strike an order

go language gui programming : use GTK( frame )
Configuration required gopath

control , parts , component
control : attribute , method
Button : Text information ( attribute ), action ( method )

Control classification :

window Put the container in
container Drop control

signal : interrupt

step :

create a window
Window settings properties
Create layout
Add layout to window
Create control ( Button )
Set control properties
Control to the layout Set coordinates ( It's abstract )
Signal processing
Show all controls

glade routine , Public sync settings size
glade technological process
1 load glade file
2 Get control
3 signal processing
4 Display controls

Drawing events can be triggered without doing anything

Any visual change will automatically trigger the drawing event
Which part of the change , Which part to refresh
It can be considered as a refresh

Drawing , Find a painter , Draw in the drawing area
w,h, -1,-1 According to the original

( The content of this note is collected from Internet resources , Encroachment and deletion )

Technology
©2019-2020 Toolsou All rights reserved,
tkinter Implementation of user login interface Vue Invalid render dynamic component in subcomponent ( First invalid , Valid for the second time )9 ride 9 Sudoku C/C++ Memory model TypeScript actual combat -12-TS The mechanism of type checking - Type inference Java Thread safety and insecurity JLink,STLink,DAPLink,CMSIS DAP Differences in use Chapter 10 use SpringMVC Frame transformation of supermarket order management system -1C Language calculation of deposit interest About the Blue Bridge Cup , Things you should know !