Golang : GTK Input dialog box examples
Here are some simplified examples on how to create input dialog box with GTK and Golang. Basically, what each programs does, is to invoke an empty dialog box and insert an input field or dropdown selection into the dialog box.
Below is the simplest way to get user input with GTK and Golang.
package main
import (
"fmt"
"github.com/mattn/go-gtk/glib"
"github.com/mattn/go-gtk/gtk"
)
func main() {
gtk.Init(nil)
window := gtk.NewWindow(gtk.WINDOW_TOPLEVEL)
window.SetPosition(gtk.WIN_POS_CENTER)
window.SetTitle("GTK Go Input Dialog example!")
window.SetIconName("gtk-dialog-info")
window.Connect("destroy", func(ctx *glib.CallbackContext) {
println("got destroy!", ctx.Data().(string))
gtk.MainQuit()
}, "Happy coding!")
dialog := gtk.NewDialog()
dialog.SetTitle("User input")
vbox := dialog.GetVBox()
label := gtk.NewLabel("Enter some characters here :")
vbox.Add(label)
input := gtk.NewEntry()
input.SetEditable(true)
vbox.Add(input)
button := gtk.NewButtonWithLabel("OK")
button.Connect("clicked", func() {
fmt.Println("Input : ", input.GetText())
//gtk.MainQuit()
})
vbox.Add(button)
quitButton := gtk.NewButtonWithLabel("Quit")
quitButton.Connect("clicked", func() {
fmt.Println("Quiting now ...")
gtk.MainQuit()
})
vbox.Add(quitButton)
dialog.ShowAll()
window.Add(dialog)
gtk.Main()
}
To set the input field initial value, use the SetText()
function. See comment below.
package main
import (
"fmt"
"github.com/mattn/go-gtk/glib"
"github.com/mattn/go-gtk/gtk"
)
func main() {
gtk.Init(nil)
window := gtk.NewWindow(gtk.WINDOW_TOPLEVEL)
window.SetPosition(gtk.WIN_POS_CENTER)
window.SetTitle("GTK Go Input Dialog example!")
window.SetIconName("gtk-dialog-info")
window.Connect("destroy", func(ctx *glib.CallbackContext) {
println("got destroy!", ctx.Data().(string))
gtk.MainQuit()
}, "Happy coding!")
dialog := gtk.NewDialog()
dialog.SetTitle("User input")
vbox := dialog.GetVBox()
label := gtk.NewLabel("Enter some characters here :")
vbox.Add(label)
input := gtk.NewEntry()
input.SetEditable(true)
input.SetText("Initial value 123") // <--- set initiate value
fmt.Println("Set initial value 123")
vbox.Add(input)
button := gtk.NewButtonWithLabel("OK")
button.Connect("clicked", func() {
fmt.Println("Input : ", input.GetText())
//gtk.MainQuit()
})
vbox.Add(button)
quitButton := gtk.NewButtonWithLabel("Quit")
quitButton.Connect("clicked", func() {
fmt.Println("Quiting now ...")
gtk.MainQuit()
})
vbox.Add(quitButton)
dialog.ShowAll()
window.Add(dialog)
gtk.Main()
}
Apart from input with string value, you can select a predefined values as well. Here is an example of how to create drop down and select input dialog box
package main
import (
"fmt"
"github.com/mattn/go-gtk/glib"
"github.com/mattn/go-gtk/gtk"
)
func main() {
gtk.Init(nil)
window := gtk.NewWindow(gtk.WINDOW_TOPLEVEL)
window.SetPosition(gtk.WIN_POS_CENTER)
window.SetTitle("GTK Go Input Dialog example!")
window.SetIconName("gtk-dialog-info")
window.Connect("destroy", func(ctx *glib.CallbackContext) {
println("got destroy!", ctx.Data().(string))
gtk.MainQuit()
}, "Happy coding!")
dialog := gtk.NewDialog()
dialog.SetTitle("User input")
vbox := dialog.GetVBox()
combos := gtk.NewHBox(false, 1)
combobox := gtk.NewComboBoxNewText()
combobox.AppendText("Yes")
combobox.AppendText("No")
combobox.SetActive(0) // yes
combobox.Connect("changed", func() {
fmt.Println("value:", combobox.GetActiveText())
if combobox.GetActiveText() == "Yes" {
fmt.Println("Yes")
} else {
fmt.Println("No")
}
})
combos.Add(combobox)
vbox.Add(combos)
quitButton := gtk.NewButtonWithLabel("Quit")
quitButton.Connect("clicked", func() {
fmt.Println("Quiting now ...")
gtk.MainQuit()
})
vbox.Add(quitButton)
dialog.ShowAll()
dialog.SetSizeRequest(400, 50)
window.Add(dialog)
gtk.Main()
}
See also : Golang : Simple image viewer with Go-GTK
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+6.6k Golang : Experimental emojis or emoticons icons programming language
+26.7k Golang : Find files by extension
+10.1k Golang : Text file editor (accept input from screen and save to file)
+6.8k Golang : Calculate BMI and risk category
+12.5k Golang : Transform comma separated string to slice example
+4.9k Golang : Get a list of crosses(instruments) available to trade from Oanda account
+22k Golang : Print leading(padding) zero or spaces in fmt.Printf?
+9.6k Golang : List available AWS regions
+12.8k Python : Convert IPv6 address to decimal and back to IPv6
+19.1k Golang : Populate dropdown with html/template example
+12.2k Golang : Display list of countries and ISO codes
+12.5k Golang : Send data to /dev/null a.k.a blackhole with ioutil.Discard