Display a GTK window


Figure 1: Display GTK-3 window

Simple GTK application load a window from a glade file and exit on hitting the window close button, we use the getobject method to get the window by name then use connect to attach to the close and destroy events.

There are lots of event we can connect to like mouse click key press and window minimise maximise check the gtk docs or glade for a list of possible event for each widget.

from gi.repository import Gtk

class application_gui:
    """Tutorial 01 Create and destroy a window"""

    def __init__(self):
	#load in our glade interface
	xml = Gtk.Builder()

	#grab our widget using get_object this is the name of the widget from glade, window1 is the default name
	self.window = xml.get_object('window1')

	#connect to events, in this instance just quit our application
	self.window.connect('delete_event', Gtk.main_quit)
	self.window.connect('destroy', lambda quit: Gtk.main_quit())

	#show the window else there is nothing to see :)

application = application_gui()