rss_feed

Display a GTK window

homeHome
pagesGTK-3
pagespython
pageswindow

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/python
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()
        xml.add_from_file('tut01.glade')
        
        #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 :)
        self.window.show()

application = application_gui()
Gtk.main()