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