Developing applications with Python 3, Glade and GTK + 3 on Windows

Introduction

In GNU / Linux developing applications with Python 3, Glade and GTK + 3 is very easy, the packages come by default in most distributions.

Thanks to Glade we can create user interfaces quickly and easily and then use them in conjunction with Python. To achieve this you use PyGObject thanks to the introspection system that GNOME offers, which makes Rapid Application Development (RAD) much easier; it is also possible to use our interfaces created with Glade from other programming languages.

The problem occurs when we want to use our applications in Microsoft Windows, since until recently there were no official packages for this system.

With this tutorial it is intended that the applications that have been made in GNU / Linux using Python 3 and GTK + 3 run without problems in Windows.

Requirements

  • Python 3.3
  • Gtk + 3
  • Glade 3.14 or higher (GUI designer)
  • pygobject

Installation on Windows

It will start by creating a folder called or another one of your preference and we will save in it all the packages that are downloaded.

Download installation packages

Download Python 3.3

It can be downloaded from the official Python page python.org

Download Python installer

Download Python installer

By clicking the link (Windows Installer) the Python installer will download.

Download Glade

Download site: glade.gnome.org

Both Python and Glade versions are 32-bit, but run without problems on 64-bit systems

Glade Page

Glade Page

Download PyGObject

Download site: https://wiki.gnome.org/PyGObject

We must download the version for GTK + 3

PyGObject

PyGObject

PyGObject in Google code

PyGObject in Google code

Installing the packages

Well up to this point we already have all the downloaded packages in the folder and as we have downloaded each package we will start first with the Python installation.

Downloaded packages

Downloaded packages

Python installation

Installation is very simple, the wizard will guide us through the process; We leave all the options marked by default.

Python installation program

Python installation program

As seen in the image, Python installs in the folder C: \ Python33 \ By default, we leave it as is and continue with the installation.

Python installation folder

Python installation folder

At this point of the installation we select the option Add python.exe to Path, with the purpose that when we launch a Python interpreter it is executed without the need to add it to the system path manually.

Add Python to the System Path

Add Python to the System Path

Then we finish the installation and Python will have been installed on our system.

Glade Installation

Glade installation does not represent a major complication, we run the installation program and follow the steps of the wizard.

Install glade

Install glade

PyGObject installation

We had downloaded the package pygi-aio-3.4.2rev11.7z, this package includes PyGObject for Python 3.3 and GTK + 3 libraries for Windows, which is compressed with 7-zip, we unzip it and we will have a folder with the following content:

Contents of the pygi-aio-3.4.2rev11.7 folder

Contents of the pygi-aio-3.4.2rev11.7 folder

Now we copy the folder gtk a C: \ Python33 \ Lib \ site-packages which is where the third party packages for Python are installed.

Copy gtk folder

Copy gtk folder

Gtk folder at C: \ Python33 \ Lib \ site-packages

Gtk folder at C: \ Python33 \ Lib \ site-packages

We go back to our folder pygi-aio-3.4.2rev11 and we open the folder py33 for the Python version that is 3.3

Contents of the py33 folder

Contents of the py33 folder

select all content Folder py33 and we copy it back into the folder C: \ Python33 \ Lib \ site-packages, we will be asked to mix and overwrite, we answer affirmatively. The contents of the folder site-packages it would be as shown in the following image:

Copy the contents of the py33 folder to C: \ Python33 \ Lib \ site-packages

Copy the contents of the py33 folder to C: \ Python33 \ Lib \ site-packages

Checking the correct installation of PyGobject and GTK + 3

To verify that our installation has been correct, we open the Python IDLE and import the GTK + 3 libraries and if we do not have any error message everything will have been done correctly.

from gi.repository import Gtk

PyGobject and GTK + 3 installed correctly

PyGobject and GTK + 3 installed correctly


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   j said

    And why GTK? Wouldn't it be better to develop in QT? besides being multiplatform and many say that it is the future

    1.    artus said

      Well, the two great graphic libraries on the GNU / Linux desktop are GTK and QT, each one chooses the one they prefer. There is no reason to fight.

      The difference between GTK and QT is that the latter is a framework, very good indeed, but being a framework it consumes a little more resources.

      For example Mozilla Firefox, Google Chrome, LibreOffice and a long etcetera are made with GTK; This does not mean that it is better than QT, everyone should choose the one that suits their preferences and needs, we do not want to cause a discussion about it.

      1.    pandev92 said

        Google chrome from version 33, leave gtk.

  2.   Rolo said

    and why the explanation for windows and not linux

    1.    Germán said

      Is it because in Linux everything is already packaged and in the official repositories of each distro? In Fedora, for example, I already have Python 3, Gtk + 3, and PyGobject installed by default. If I want Glade, it's just "yum install glade". Easy right? 🙂

    2.    artus said

      Because as Germán says, for GNU / Linux distributions they are already packaged and it is very easy to install them; for example to install glade in Debian is very simple:
      aptitude install glade

  3.   Marcelo said

    If I want to use Python 2.7 will the steps be the same?
    Obviously, installing the 2.7-bit Python 32 (even if you use the 64-bit OS) and instead of the py33 folder, the 2.7 would go. Would it work?
    Thank you.

    1.    artus said

      Well yes, if you have a problem, leave me a comment.

      Luck.

      1.    Marcelo said

        Thanks, I've already tried and for now the "Hello world" works for me.

        I see if I can get any good pyGTK3 tutorial or whatever it's called and any problems, warning.

          1.    Marcelo said

            Caramba! Thanks a lot!

          2.    Marcelo said

            Testing GTK3 with Python 2.7 on Windows and following the tutorial. So far all programs work for me, except the one in Chapter 14 (IconView -> https://python-gtk-3-tutorial.readthedocs.org/en/latest/iconview.html)

            It gives me the following error:
            Traceback (most recent call last):
            File "C: \ Users \ User \ Desktop \ test.py", line 24, in
            win = IconViewWindow ()
            File "C: \ Users \ User \ Desktop \ test.py", line 19, in __init__
            pixbuf = Gtk.IconTheme.get_default (). load_icon (icon, 64, 0)
            File "C: \ Python27 \ lib \ site-packages \ gi \ types.py", line 47, in function
            return info.invoke (* args, ** kwargs)
            GError: Icon 'gtk-cut' not present in theme

            I tried it with other icons and it doesn't work either. Do I need to install a theme? Thanks since now.

          3.    Marcelo said

            Neither does Example 19. Drag and Drop.

            The function that calls in both cases and that is where I get an error is:

            pixbuf = Gtk.IconTheme.get_default (). load_icon (icon_name, 16, 0)

            I can't find the icon, but in other examples you can use them. It only gives me an error when I want to use it with that function (here icon_name can be replaced with a value such as Gtk.STOCK_CUT or whatever and it gives exactly the same error).

          4.    Marcelo said

            I keep talking to myself. What works for me in other examples is that the images load on the buttons. I can't load the icons with the function that comes in the two examples I gave earlier.

          5.    artus said

            I think the error is clear:
            GError: Icon 'gtk-cut' not present in theme

            Apparently that icon is not available, use another icon. Probably the package maintainer didn't include the theme icons or it can't be found in the default path, use a GNU / Linux distribution and see if the same error still appears.

            Regarding question 19, it seems to be related to the PyGobject version, at the top of the documentation it says:

            Footnotes
            Versions of PyGObject = 3.0.3 is required for the following examples to work.

            The version you have is 3.0, try a GNU / Linux distribution; and then we have to wait for them to update the package (for Windows) or in any case you can compile it yourself.

            Greetings and move on.

          6.    Marcelo said

            In Ubuntu all the tutorial examples work perfect for me. On Windows, it still doesn't work. I will have to wait. Another that does not walk is the example of the Spinner, which does not do the animation. Greetings and thanks.

  4.   Yoshua said

    I have followed the installation tutorial and installed everything step by step.
    And executing the following code:
    from gi.repository import Gtk

    class MainParty:
    def __init __ (self):
    filename = "/ data / dam / Python exemplars /Saudoform"
    constructor = Gtk.builder ()
    constructor.add_from_file (filename)
    #dictionary where we relate sinais with events
    sinais = {
    «ClickedAccept»: self.clic_boton,
    "ActivateTextCadro": self.clic_boton,
    "DestroyFiestra": Gtk.main_quit

    }
    constructor.connect_signals (sinais)
    #We get a reference to the XML descriptor to be able to access
    self.label = self.widgets_widget ("Label")
    self.cadroTexto = self.widgets_widget ("CadroTexto")

    def button_click (self, widget):
    text = self.cadroTexto.get_text ()
    self.label.set_text ("wave% s"% text)

    if __name__ == »__ main__»:
    FiestraPrincipal ()
    Gtk.Main ()

    I get this answer:
    Traceback (most recent call last):
    File «C: / Users / admin / Google Drive / test / SaudoForm.py», line 3, in
    from gi.repository import Gtk
    File "C: \ Python33 \ lib \ site-packages \ gi \ __ init__.py", line 27, in
    from ._gi import _API, Repository
    ImportError: DLL load failed:% 1 is not a valid Win32 application.

    Someone knows what the problem is, or what may be its possible solution.
    Thank you very much.

  5.   Jesus said

    Interesting. I had done the installation in linux, but the explanation for windows is fine, now I have it in both. 😉

  6.   Rafael Carmona said

    It's funny on a 7-bit windows 32 the first time.
    On a Windows 7 64-bit, I can't make it run, I always get it;
    >>> from gi.repository import Gtk
    Traceback (most recent call last):
    File «», line 1, in
    File "C: \ Python33 \ lib \ site-packages \ gi \ __ init__.py", line 27, in
    from ._gi import _API
    ImportError: DLL load failed: The specified process was not found.

    I have tried it anyway possible, I already lost the illusion of installing it.

    1.    artus said

      You will see the 64-bit libraries still have some bugs, please install the 32-bit version of both python and GTK + so you don't have problems.

      You should never lose the illusion 🙂

  7.   Richard said

    Hi I'm working with python + gtk3, it's very good now I'm in a little problem I want to change the icon to my form (window) in this case and I can change it, thanks in advance, Greetings

  8.   jors said

    publish simple videos of how to program in gtk + 3 and simple tutorials greetings

  9.   Jose said

    Excellent tutorial. Thank you very much for your effort. Regards.

  10.   jkmilo1030 said

    I did all the steps and when I went to trust the installation I got this error.

    Traceback (most recent call last):
    File «», line 1, in
    from gi.repository import Gtk
    ImportError: No module named 'gi'