| 1 |
.. currentmodule:: gi.repository |
| 2 |
|
| 3 |
Getting Started |
| 4 |
=============== |
| 5 |
|
| 6 |
This section will give you a first contact with GTK, explaining each line of |
| 7 |
code. |
| 8 |
|
| 9 |
.. seealso:: `Getting Started with GTK`_ in the GTK documentation. |
| 10 |
|
| 11 |
Basic Example |
| 12 |
------------- |
| 13 |
|
| 14 |
To start with our tutorial we create the simplest program possible. |
| 15 |
This program will create an empty 200 x 200 pixel window. |
| 16 |
|
| 17 |
.. image:: images/basic_example.png |
| 18 |
|
| 19 |
.. literalinclude:: examples/simple_example.py |
| 20 |
:linenos: |
| 21 |
|
| 22 |
We will now explain each line of the example. |
| 23 |
|
| 24 |
.. literalinclude:: examples/simple_example.py |
| 25 |
:lines: 1-4 |
| 26 |
|
| 27 |
In the beginning, we have to import the Gtk module to be able to access GTK's |
| 28 |
classes and functions. |
| 29 |
Since a user's system can have multiple versions of GTK installed at the same, |
| 30 |
we want to make sure that when we import Gtk that it refers to GTK4 |
| 31 |
and not any other version of the library, which is the purpose |
| 32 |
of the statement ``gi.require_version('Gtk', '4.0')``. |
| 33 |
|
| 34 |
.. literalinclude:: examples/simple_example.py |
| 35 |
:lines: 14-15 |
| 36 |
|
| 37 |
Now we create an application. The ``application_id`` argument is needed as |
| 38 |
method of identifying your application to the system, if you do not know what |
| 39 |
to set please :devdocs:`check this tutorial <tutorials/application-id>`. |
| 40 |
|
| 41 |
We are also connecting to the application's ``activate`` event, where we will |
| 42 |
create the window. |
| 43 |
|
| 44 |
.. literalinclude:: examples/simple_example.py |
| 45 |
:lines: 7,9-10 |
| 46 |
|
| 47 |
In the ``on_activate`` callback we will create an empty window, and then display |
| 48 |
it using the :meth:`Gtk.Window.present` method. |
| 49 |
|
| 50 |
.. literalinclude:: examples/simple_example.py |
| 51 |
:lines: 18 |
| 52 |
|
| 53 |
Finally, we start the application using the :meth:`Gio.Application.run` method. |
| 54 |
|
| 55 |
|
| 56 |
Minimal Example without GtkApplication |
| 57 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 58 |
|
| 59 |
In GTK 4, if you don't want to use :class:`Gtk.Application` you can use this |
| 60 |
replacement that will iterate the default main loop until all windows have been |
| 61 |
closed: |
| 62 |
|
| 63 |
.. attention:: |
| 64 |
Doing this is only useful for very specific use cases like tests. For app |
| 65 |
development you should always use :class:`Gtk.Application`. |
| 66 |
|
| 67 |
.. code:: python |
| 68 |
|
| 69 |
import gi |
| 70 |
gi.require_version('Gtk', '4.0') |
| 71 |
from gi.repository import GLib, Gtk |
| 72 |
|
| 73 |
win = Gtk.Window() |
| 74 |
win.present() |
| 75 |
|
| 76 |
while (len(Gtk.Window.get_toplevels()) > 0): |
| 77 |
GLib.MainContext.default().iteration(True) |
| 78 |
|
| 79 |
.. _extended-example: |
| 80 |
|
| 81 |
Extended Example |
| 82 |
---------------- |
| 83 |
|
| 84 |
For something a little more useful, here's the PyGObject version of the classic |
| 85 |
"Hello World" program. |
| 86 |
|
| 87 |
.. image:: images/extended_example.png |
| 88 |
|
| 89 |
.. literalinclude:: examples/extended_example.py |
| 90 |
:linenos: |
| 91 |
|
| 92 |
This example differs from the simple example as we sub-class |
| 93 |
:class:`Gtk.ApplicationWindow` to define our own ``MyWindow`` class. |
| 94 |
|
| 95 |
.. seealso:: |
| 96 |
For more info about subclassing read |
| 97 |
:doc:`GObject Subclassing </tutorials/gobject/subclassing>`. |
| 98 |
|
| 99 |
.. literalinclude:: examples/extended_example.py |
| 100 |
:lines: 7 |
| 101 |
|
| 102 |
In the class's constructor we have to call the constructor of the super class. |
| 103 |
In addition, we tell it to set the value of the property title to *Hello World*. |
| 104 |
Note that we are also receiving and passing ``**kargs``, so we can redirect |
| 105 |
other arguments passed on ``MyWindow`` construction to the base class. |
| 106 |
|
| 107 |
.. literalinclude:: examples/extended_example.py |
| 108 |
:lines: 8-9 |
| 109 |
|
| 110 |
The next three lines are used to create a button widget, connect to its |
| 111 |
``clicked`` signal and add it as child to the top-level window. |
| 112 |
|
| 113 |
.. literalinclude:: examples/extended_example.py |
| 114 |
:lines: 11-13 |
| 115 |
|
| 116 |
Accordingly, the method `on_button_clicked` will be called if you click on |
| 117 |
the button. In this example we are using the method to print ``Hello World`` and |
| 118 |
calling the window :meth:`Gtk.Window.close` method to close the window. |
| 119 |
|
| 120 |
.. literalinclude:: examples/extended_example.py |
| 121 |
:lines: 15-17 |
| 122 |
|
| 123 |
The last block, outside of the class, is very similar to the simple example |
| 124 |
above, but instead of creating an instance of the generic |
| 125 |
:class:`Gtk.ApplicationWindow` or :class:`Gtk.Window` class, we create an |
| 126 |
instance of ``MyWindow``. |
| 127 |
|
| 128 |
.. _Getting Started with GTK: https://docs.gtk.org/gtk4/getting_started.html |