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
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
Download PyGObject
Download site: https://wiki.gnome.org/PyGObject
We must download the version for GTK + 3
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.
Python installation
Installation is very simple, the wizard will guide us through the process; We leave all the options marked by default.
As seen in the image, Python installs in the folder C: \ Python33 \ By default, we leave it as is and continue with the installation.
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.
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.
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:
Now we copy the folder gtk a C: \ Python33 \ Lib \ site-packages which is where the third party packages for Python are installed.
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
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:
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
And why GTK? Wouldn't it be better to develop in QT? besides being multiplatform and many say that it is the future
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.
Google chrome from version 33, leave gtk.
and why the explanation for windows and not linux
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? 🙂
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
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.
Well yes, if you have a problem, leave me a comment.
Luck.
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.
Tutorial:
https://python-gtk-3-tutorial.readthedocs.org/en/latest/index.html
Caramba! Thanks a lot!
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.
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).
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.
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.
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.
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.
Interesting. I had done the installation in linux, but the explanation for windows is fine, now I have it in both. 😉
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.
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 🙂
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
publish simple videos of how to program in gtk + 3 and simple tutorials greetings
Excellent tutorial. Thank you very much for your effort. Regards.
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'