본문 바로가기
Programming/Golang

Glade를 활용한 go언어 gui프로그래밍

by Wilkyway 2020. 3. 26.
반응형

 

이번엔 gtk toolkit 전용 GUI Designer인 Glade를 이용하여 Go언어에서 활용해보도록 하겠습니다.

 

우선 Glade를 설치해야 합니다. linux에서는 비교적 쉽게 찾아서 설치 가능한데요.. 윈도우에서는 조금 설치가 쉽지 않습니다. Mingw를 설치하고 아래의 명령으로 설치를 진행해줍니다.

pacman -S mingw-w64-x86_64-glade

빠른 실행을 위해서는 sourceforge 사이트에서 예전버전을 다운받는 것도 괜찮아보입니다.

 

 

다음으로는 Glade에서 UI를 적당히 디자인 해줍니다. (glade예제파일)

생각해보니...그냥 적당히는 안될 것 같네요.

 

 

1. 최상위 - > GtkWindow 를 선택해주세요. 그리고 아이디를 main_window,  사이즈를 440, 250 으로 설정합니다.

2. 컨테이너-> GtkHeaderBar를 선택해주세요. 아이디를 headerbar, 간격 7, 항목 3으로 설정해주세요

3. 컨트롤->GtkButton 3개를 생성해서 아이디를 TestBtn, HelloBtn, exitBtn으로 해주세요. 그리고 그에 맞게 레이블도 수정해 줍니다.

이렇게 구성하면 glade2.glade 로 저장합니다. 물론 아무 이름이나 상관 없습니다만, 그럴 경우 아래 소스코드의 파일명 부분을 적당히 수정해주세요. 이제 아래의 소스코드를 수행해봅니다.

// gotk_glade.go
package main

import (
	"errors"
	"fmt"
	"log"
	"os"
	"reflect"

	"github.com/gotk3/gotk3/glib"
	"github.com/gotk3/gotk3/gtk"
)

var headerBar *gtk.HeaderBar

func main() {
	// Create a new application.
	application, err := gtk.ApplicationNew("new.test", glib.APPLICATION_FLAGS_NONE)
	errorCheck(err)

	// Connect function to application startup event, this is not required.
	application.Connect("startup", func() {
		log.Println("application startup")
	})

	// Connect function to application activate event
	application.Connect("activate", func() {
		log.Println("application activate")

		// Get the GtkBuilder UI definition in the glade file.
		builder, err := gtk.BuilderNewFromFile("glade2.glade")
		errorCheck(err)

		// Map the handlers to callback functions, and connect the signals
		// to the Builder.
		signals := map[string]interface{}{
			"on_main_window_destroy": onMainWindowDestroy,
			"on_HelloBtn_clicked":    clickedTestButton,
		}
		builder.ConnectSignals(signals)

		// Get the object with the id of "main_window".
		obj, err := builder.GetObject("main_window")
		fmt.Println(reflect.TypeOf(obj))
		errorCheck(err)

		// Verify that the object is a pointer to a gtk.ApplicationWindow.
		win, err := isWindow(obj)
		fmt.Println(reflect.TypeOf(win))
		errorCheck(err)

		headerBar, err := builder.GetObject("headerbar")
		errorCheck(err)
		fmt.Println(reflect.TypeOf(headerBar))
		//headerBar.SetTitle("sss")

		// Show the Window and all of its components.
		win.Show()
		application.AddWindow(win)
	})

	// Connect function to application shutdown event, this is not required.
	application.Connect("shutdown", func() {
		log.Println("application shutdown")
	})

	// Launch the application
	os.Exit(application.Run(os.Args))
}

func isWindow(obj glib.IObject) (*gtk.Window, error) {
	// Make type assertion (as per gtk.go).
	if win, ok := obj.(*gtk.Window); ok {
		return win, nil
	}
	return nil, errors.New("not a *gtk.Window")
}

func errorCheck(e error) {
	if e != nil {
		// panic for any errors.
		log.Panic(e)
	}
}

// onMainWindowDestory is the callback that is linked to the
// on_main_window_destroy handler. It is not required to map this,
// and is here to simply demo how to hook-up custom callbacks.
func onMainWindowDestroy() {
	log.Println("onMainWindowDestroy")
}

func clickedTestButton() {
	fmt.Println("Testclick")
	//headerBar.SetTitle("New Title!!")
}


모두들 성공하시길 빌겠습니다.
그럼 이만~~

 

반응형

댓글