Signals

Did you notice that, in our hello world example, the Quit button would exit the application, but that closing the window did not? That's because we didn't connect any callback to the window destroy signal. We can fix this by adding the following lines to handle this case:

window.Connect("destroy", func() {
gtk.MainQuit()
})

This code connects the provided anonymous function to the destroy signal of window. When the signal is emitted, the function is called and the application will now exit correctly. As the gtk.MainQuit() function takes 0 parameters, we could write the same more concisely as follows:

window.Connect("destroy", gtk.MainQuit)

But hold on a moment, how come the button click worked? That's because we used the Clicked() function on the button component. This is a convenience function that sets up the signal connection for you (and keeps the code a little neater!). If you look at the source code for the Button.Clicked() function, you will see what happens:

func (v *Button) Clicked(onclick interface{}, datas ...interface{}) int {
return v.Connect("clicked", onclick, datas...)
}

And so, you can see it is not always essential to wire these connections manually as Go-GTK provides many convenience methods like this one.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.176.145