Debian Codegraph @

docs/getting_started.rst

raw
1 .. include:: icons.rst
2
3 .. _gettingstarted:
4
5 ===============
6 Getting Started
7 ===============
8
9 To get things started we will try to run a very simple `GTK
10 <https://www.gtk.org/>`_ based GUI application using the :doc:`PyGObject <index>` provided
11 Python bindings. First create a small Python script called ``hello.py`` with
12 the following content and save it somewhere:
13
14 .. code:: python
15
16 import gi
17
18 gi.require_version("Gtk", "4.0")
19 from gi.repository import GLib, Gtk
20
21
22 class MyApplication(Gtk.Application):
23 def __init__(self):
24 super().__init__(application_id="com.example.MyGtkApplication")
25 GLib.set_application_name('My Gtk Application')
26
27 def do_activate(self):
28 window = Gtk.ApplicationWindow(application=self, title="Hello World")
29 window.present()
30
31
32 app = MyApplication()
33 app.run()
34
35 Before we can run the example application we need to install PyGObject, GTK,
36 and their dependencies. Follow the instructions for your platform below or use
37 a cross-platform package manager like `conda <https://docs.conda.io/>`_ or
38 `Pixi <https://pixi.sh/>`_.
39
40 For full IDE support (incl. autocomplete) you will also need typing stubs
41 available here `PyGObject-stubs <https://pypi.org/project/PyGObject-stubs/>`_.
42
43 ======================================================= ==================================================== ==========================================================
44 |ubuntu-logo| :ref:`Ubuntu <ubuntu-getting-started>` |fedora-logo| :ref:`Fedora <fedora-getting-started>` |arch-logo| :ref:`Arch Linux <arch-getting-started>`
45 |windows-logo| :ref:`Windows <windows-getting-started>` |macosx-logo| :ref:`macOS <macosx-getting-started>` |opensuse-logo| :ref:`openSUSE <opensuse-getting-started>`
46 |conda-logo| :ref:`Conda <conda-getting-started>` |pixi-logo| :ref:`Pixi <pixi-getting-started>` |source-logo| :ref:`from Source <devenv>`
47 ======================================================= ==================================================== ==========================================================
48
49 After running the example application have a look at the :doc:`/tutorials/index`
50 for an overview of how to create GTK apps and the `GNOME Python API docs
51 <https://api.pygobject.gnome.org/>`__ for API documentation for all supported
52 libraries.
53
54
55 .. _windows-getting-started:
56
57 |windows-logo| Windows
58 ----------------------
59
60 #) Go to https://www.msys2.org/ and download the x86_64 installer
61 #) Follow the instructions on the page for setting up the basic environment
62 #) Run ``C:\msys64\ucrt64.exe`` - a terminal window should pop up
63 #) Execute ``pacman -Suy``
64 #) Execute ``pacman -S mingw-w64-ucrt-x86_64-gtk4 mingw-w64-ucrt-x86_64-python mingw-w64-ucrt-x86_64-python-gobject``
65
66 .. note::
67 While some systems may use ``python3`` in the package name, current MSYS2 UCRT64
68 environments typically use ``python``. If one fails, try the other (incl. step 8).
69 #) To test that GTK is working you can run ``gtk4-demo``
70 #) Copy the ``hello.py`` script you created to ``C:\msys64\home\<username>``
71 #) In the mingw32 terminal execute ``python hello.py`` - a window should appear.
72
73 .. figure:: images/start_windows.png
74 :scale: 60%
75
76
77 .. _ubuntu-getting-started:
78
79 |ubuntu-logo| Ubuntu / |debian-logo| Debian
80 -------------------------------------------
81
82 Installing the system provided PyGObject:
83 #) Open a terminal
84 #) Execute ``sudo apt install python3-gi python3-gi-cairo gir1.2-gtk-4.0``
85 #) Change the directory to where your ``hello.py`` script can be found (e.g. ``cd Desktop``)
86 #) Run ``python3 hello.py``
87
88 Installing from PyPI with pip:
89 #) Open a terminal and enter your virtual environment
90 #) Execute ``sudo apt install libgirepository-2.0-dev gcc libcairo2-dev pkg-config python3-dev gir1.2-gtk-4.0``
91 to install the build dependencies and GTK
92 #) Execute ``pip3 install pycairo`` to build and install Pycairo
93 #) Execute ``pip3 install PyGObject`` to build and install PyGObject
94 #) Change the working directory to where your ``hello.py`` script can be found
95 #) Run ``python3 hello.py``
96
97 |start-linux|
98
99 .. _fedora-getting-started:
100
101 |fedora-logo| Fedora
102 --------------------
103
104 Installing the system provided PyGObject:
105 #) Open a terminal
106 #) Execute ``sudo dnf install python3-gobject gtk4``
107 #) Change the working directory to where your ``hello.py`` script can be found
108 #) Run ``python3 hello.py``
109
110 Installing from PyPI with pip:
111 #) Open a terminal and enter your virtual environment
112 #) Execute ``sudo dnf install gcc gobject-introspection-devel cairo-gobject-devel pkg-config python3-devel gtk4``
113 to install the build dependencies and GTK
114 #) Execute ``pip3 install pycairo`` to build and install Pycairo
115 #) Execute ``pip3 install PyGObject`` to build and install PyGObject
116 #) Change the working directory to where your ``hello.py`` script can be found
117 #) Run ``python3 hello.py``
118
119
120 .. _arch-getting-started:
121
122 |arch-logo| Arch Linux
123 ----------------------
124
125 Installing the system provided PyGObject:
126 #) Open a terminal
127 #) Execute ``sudo pacman -S python-gobject gtk4``
128 #) Change the working directory to where your ``hello.py`` script can be found
129 #) Run ``python3 hello.py``
130
131 Installing from PyPI with pip:
132 #) Open a terminal and enter your virtual environment
133 #) Execute ``sudo pacman -S python cairo pkgconf gobject-introspection gtk4``
134 to install the build dependencies and GTK
135 #) Execute ``pip3 install pycairo`` to build and install Pycairo
136 #) Execute ``pip3 install PyGObject`` to build and install PyGObject
137 #) Change the working directory to where your ``hello.py`` script can be found
138 #) Run ``python3 hello.py``
139
140
141 .. _opensuse-getting-started:
142
143 |opensuse-logo| openSUSE
144 ------------------------
145
146 Installing the system provided PyGObject:
147 #) Open a terminal
148 #) Execute ``sudo zypper install python3-gobject python3-gobject-Gdk typelib-1_0-Gtk-4_0 libgtk-4-1``
149 #) Change the directory to where your ``hello.py`` script can be found
150 #) Run ``python3 hello.py``
151
152 Installing from PyPI with pip:
153 #) Open a terminal and enter your virtual environment
154 #) Execute ``sudo zypper install cairo-devel pkg-config python3-devel gcc gobject-introspection-devel``
155 to install the build dependencies and GTK
156 #) Execute ``pip3 install pycairo`` to build and install Pycairo
157 #) Execute ``pip3 install PyGObject`` to build and install PyGObject
158 #) Change the working directory to where your ``hello.py`` script can be found
159 #) Run ``python3 hello.py``
160
161
162 .. _macosx-getting-started:
163
164 |macosx-logo| macOS
165 -------------------
166
167 #) Go to https://brew.sh/ and install homebrew
168 #) Open a terminal
169 #) Execute ``brew install pygobject3 gtk4``
170 #) Change the working directory to where your ``hello.py`` script can be found
171 #) Run ``python3 hello.py``
172
173 .. figure:: images/start_macos.png
174 :scale: 70%
175
176 .. _conda-getting-started:
177
178 |conda-logo| conda
179 ------------------
180
181 #) Go to https://docs.conda.io and install Miniconda or Miniforge
182 #) Open a terminal
183 #) Execute ``conda create --channel conda-forge --name pygobject pygobject gtk4``
184 #) Execute ``conda activate pygobject``
185 #) Change the working directory to where your ``hello.py`` script can be found
186 #) Run ``python hello.py``
187
188 |start-conda|
189
190 .. _pixi-getting-started:
191
192 |pixi-logo| Pixi
193 ----------------
194
195 #) Go to https://pixi.sh/latest/installation/ and install pixi
196 #) Place your ``hello.py`` script into a separate ``hello`` directory
197 #) Open a terminal and ``cd hello``
198 #) Execute ``pixi init .``
199 #) Execute ``pixi add pygobject gtk4``
200 #) Run ``pixi run python hello.py``
201
202 |start-pixi|
203
204 ----
205
206 For more details on how to use a virtualenv with PyGObject, see the
207 ":ref:`devenv`" page.
208
209 .. toctree::
210 :hidden:
211 :maxdepth: 1
212 :caption: What's new
213
214 news/pygobject-3-56
215
216 .. toctree::
217 :hidden:
218 :maxdepth: 1
219 :caption: Project Information
220
221 changelog
222 bugs_repo