1.8 KiB
Executable File
1.8 KiB
Executable File
Display a GTK window
#+THUMBNAIL:../../../images/gtk/tut01-windows.png
.. title: Display a GTK window .. slug: 01-displaying-a-gtk-window .. date: 2014-01-05 12:00:00 UTC .. tags: GTK-3, python, window .. category: python .. description: GTK Example on loading a window from a glade fiel and displaying it on the screen. .. type: text

Simple GTK application load a window from a glade file and exit on hitting the window close button, we use the get_object 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.
#!/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()