Home > Backend Development > Golang > How to build gtk in golang

How to build gtk in golang

PHPz
Release: 2023-04-25 13:56:13
Original
1421 people have browsed it

Overview

As Golang becomes more and more popular, more and more people are trying to use Golang to build desktop applications. Gtk is a popular toolkit for creating cross-platform GUIs. This article will introduce how to use Golang and Gtk to quickly build GUI applications, and demonstrate the use of some basic Gtk controls.

Step 1: Install Gtk

The first task to complete is to install Gtk3. This can be done in many ways, but the following is how to install it on an Ubuntu or Debian system:

 $ sudo apt-get update
 $ sudo apt-get install libgtk-3-dev
Copy after login

Step 2: Install related packages

In order to use Golang to write Gtk applications, you need to install the following packages:

 $ go get -u github.com/gotk3/gotk3/gtk
 $ go get -u github.com/gotk3/gotk3/glib
 $ go get -u github.com/gotk3/gotk3/cairo
Copy after login

Step 3: Create the Main function

Next , you need to create a main function to define the windows, frames, etc. that need to be created. The following is an example of a simple Main function:

 package main

 import (
  "github.com/gotk3/gotk3/gtk"
  "log"
 )

 func main() {
  // 初始化 Gtk
  gtk.Init(nil)

  // 创建新的窗口
  win, err := gtk.WindowNew(gtk.WINDOW_TOPLEVEL)
  if err != nil {
    log.Fatal("无法创建窗口:", err)
  }

  // 设置窗口的标题
  win.SetTitle("我的第一个Golang Gtk应用程序")

  // 设置窗口的大小
  win.SetDefaultSize(800, 600)

  // 显示窗口
  win.Show()

  // 进入主循环
  gtk.Main()
 }
Copy after login

In this example, the most important task of the Main function is to create a new top-level window using gtk.WindowNew. This function requires a gtk.WindowType parameter to specify the type of window. In this case, we just need to specify gtk.WINDOW_TOPLEVEL as this parameter, which will create a simple window without menu bar and toolbar.

Once the window is created, we can use win.SetTitle and win.SetDefaultSize to set the window's title and size. Finally, we use win.Show to display the window on the screen. Also note that we call the gtk.Main() loop at the end, which will keep the window open until the user closes it.

Step 4: Create a Box

Now, we have created a simple window, but it is a blank canvas. We need to add some controls to the window to better organize the view. In Gtk this can be easily done by adding the control to the Box.

Gtk provides four different types of boxes: GtkBox, GtkHBox, GtkVBox and GtkFrame. GtkBox is a basic box that can be used to arrange controls horizontally or vertically. GtkHBox and GtkVBox are just for performing common horizontal and vertical layouts. GtkFrame is a special box that provides labels and shadows.

The following is an example of adding a GtkBox to a new window:

 func main() {
     gtk.Init(nil)
     win, err := gtk.WindowNew(gtk.WINDOW_TOPLEVEL)
     if err != nil {
         log.Fatal("无法创建窗口:", err)
     }
     win.SetTitle("我的第一个Golang Gtk应用程序")
     win.SetDefaultSize(800, 600)

     // 创建容器
     box, err := gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 4)
     if err != nil {
         log.Fatal("无法创建盒子", err)
     }

     // 向盒子添加一个标签
     label, err := gtk.LabelNew("Hello, Golang!")
     if err != nil {
         log.Fatal("无法创建标签", err)
     }
     box.PackStart(label, true, true, 0)

     // 显示窗口和盒子
     win.Add(box)
     box.ShowAll()
     win.Show()
     gtk.Main()
 }
Copy after login

In this example, we first create a vertical box. Using gtk.BoxNew, we specify gtk.ORIENTATION_VERTICAL and set the second parameter to 4 to specify the spacing between items in the box. Next, we create a new label, add it to the box, and finally set the label's text.

The last step is to display the window and box. We add the box to the window using the win.Add method and show all elements in the box using the box.ShowAll() method.

Step 5: Add buttons

In addition to labels, Gtk also supports various controls, such as buttons, text boxes, and drop-down lists. Let's add a button to our window. Here is an example of adding a button to a window:

 func main() {
     gtk.Init(nil)
     win, err := gtk.WindowNew(gtk.WINDOW_TOPLEVEL)
     if err != nil {
         log.Fatal("无法创建窗口:", err)
     }
     win.SetTitle("我的第一个Golang Gtk应用程序")
     win.SetDefaultSize(800, 600)

     box, err := gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 4)
     if err != nil {
         log.Fatal("无法创建盒子", err)
     }

     button, err := gtk.ButtonNewWithLabel("点击我!")
     if err != nil {
         log.Fatal("无法创建按钮", err)
     }

     button.Connect("clicked", func() {
         log.Println("按钮被点击了!")
     })

     box.PackStart(button, false, false, 0)

     win.Add(box)
     box.ShowAll()
     win.Show()
     gtk.Main()
 }
Copy after login

In this example, we first create a new button using gtk.ButtonNewWithLabel and set the button's text to "Click me!". We then connect the event handler and the button will output a message to the console every time the user clicks the button.

Finally, we add the button to the window, set the second parameter to false to indicate not to stretch the control, and use the box.PackStart method to add a "pixel" value of spacer.

Step Six: Add Image

Finally, we will add an image to our window, this can be easily done using GtkImage. Here is an example of adding an image to a window:

 func main() {
     gtk.Init(nil)
     win, err := gtk.WindowNew(gtk.WINDOW_TOPLEVEL)
     if err != nil {
         log.Fatal("无法创建窗口:", err)
     }
     win.SetTitle("我的第一个Golang Gtk应用程序")
     win.SetDefaultSize(800, 600)

     box, err := gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 4)
     if err != nil {
         log.Fatal("无法创建盒子", err)
     }

     button, err := gtk.ButtonNewWithLabel("点击我!")
     if err != nil {
         log.Fatal("无法创建按钮", err)
     }

     button.Connect("clicked", func() {
         log.Println("按钮被点击了!")
     })

     box.PackStart(button, false, false, 0)

     // 加载图像文件
     img, err := gtk.ImageNewFromFile("test.png")
     if err != nil {
        log.Fatal("无法加载图像", err)
     }

     box.PackStart(img, false, false, 0)

     win.Add(box)
     box.ShowAll()
     win.Show()
     gtk.Main()
 }
Copy after login

In this example, we first create a button and add it to the box. Next, we load the test.png file located on the local file system and create a new GtkImage instance using gtk.ImageNewFromFile. Finally, we add the image to our box using the box.PackStart method.

Conclusion

Gtk is a popular GUI toolkit that can be used to create cross-platform GUI applications. It is very convenient to write Gtk applications using Golang because Golang provides many third-party packages to support the use of Gtk. In this article, we introduced how to use Golang and Gtk to create windows, add common controls such as boxes, buttons, and images. I hope that through this article, you can easily get started writing GUI applications using Golang and Gtk.

The above is the detailed content of How to build gtk in golang. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template