Cross Python
Hello world
I am used to develop in a PyGTK environment, so my initial was to try to develop my application based on it. My first choice was to follow this howto, but it's outdated and doesn't work. Afterwards i went to this page and downloaded their PyGTK installer, and to the Gtk2 Dropline installer (which is not being maintained). Finally i could run my linux baked little app. Unfornatly it was spitting out some strange error messages, namely:
** (main.py:4294685485): WARNING **: Couldn't load font "MS Sans Serif 8" falling back to "Sans 8"
This would make the application run rather slow. Also my app is composed by two textviews, when i click on a button the some text appears on the second window, well in win32 it didn't. It needed some focus in order to start refreshing correctly.
I assumed it was a problem related to the version of Gtk 2.2.4. So i want to the official site but unfornatly it was down, due to a Gimp server replacement. So i went to yet another site for another shot of luck. It's funny to observe how the Gtk2 runtimes change in size. The last site i went it's only 3.5MB, whereas the installer version from Dropline is 6.5MB.
Lucky enough the glade DLL was missing so i found out Glade Win32 project, which apparently also has a package which bundles Gtk2 in it. The libglade DLL was just 30kb, so after that it was just one more dependency problem: libxml2. Downloaded the missing DLL, but still it was insisting something was missing, and since i didn't have a clue what was it (don't know any ldd for win32) i crossed my fingers for the gladewin32 All in One Installer.
The All in One Installer was 7.9 MB but it was stated to have alot of demos/optional stuff. The installer actually worked, and all those warning messages stopped appearing. Unfornatly the default Gtk theme was a bit strange, but that's nothing that a bit of tweaking couldn't help.
Looking like anything else
My next task was to grok, fbornicate and tweak gtkrc in order to make it look a bit like windowish apps.
Turned out that the previous error was not due to Gtk after all, after i installed wimp the same problem appeared. Which, in turn, made me wonder if this was something that could be changed in gtkrc. With a bit of googling and i found out how to change the font name and all was working like a charm.
Making it easy to run
My next task was to try to create a standalone executable, which would eventually depend on dlls, i had two options cx_Freeze and py2exe
I had already successfully fiddled with cx_Freeze in Linux, the same can not be said about py2exe, on win32. So the logical choice was to start with the least problematic.
I even had the algorith all thought up. The way this two programs work is by picking up an embended python executable and append our python code on the end. This way i could compress the python executable and with a bit more space, if needed, which was not the case. So onto the next step, actually making the exe.
cx_Freeze had a little dependency win32api, i downloaded it and continued my persue. Creating the executable was pretty strtaightforward:
FreezePython --include-modules=gobject,atk,pango,gtk.glade --base c:\path\to\ConsoleBase.exe --install-dir dist main.py
After this i had the following error:
LookupError: no codec search functions registered: can't find encoding
My program deals with unicode text (thanks to the GtkTextView), thus this could only mean that i had to import the unicode module.
Appending the module 'codecs' made it run but it still needed to import utf8 codec. So i also appended 'encodings.utf_8' module. After this all was fine and dandy, working my own Gtk2 based application on win32, through an executable.
After this i trimmed down all needed dependencies, removing the extra dlls that were no being used, configuration files and so on. Created my one Gtk2 distribution and prepared for the next step.
Lay my egg
Now i had to start thinking on deploying my application, my first choice was looking to NSIS. I downloaded it's editor, HMNE, and started working on it.
Creating the installer was not a big problem. The problem would be making the program actually work. The first problem i encountered was the Glade file, i had to check the sys.argv[0] path and parse the basedir. This way i could retrieve the instalation base use it to locate my glade file.
After this the program was up and running on my win32 machine, so the next step would be to install it on another, with no python or gtk2 installed.
After installation i the second problem appeared, pango needs to load a config file located under etc/pango/pango.modules which holds the path of the font modules used, this is located under lib/pango/1.4.0/modules, because this is hardcoded it should be generated upon installation.
The fix was quite an easy one, i changed the absolute location of the directories to relative ones. The other problem that appeared is that the generated .modules file contained 14~1.0 instead of 1.4.1, this would cause WinXP not to recognize the modules path.
Summing it all up
In the end, despite this taking a hole day to take make it working, it wasn't all that hard :) And i can now enjoy my Linux backed application on the evil world, built 100% with free software! Gtk2 + Python prooved to be an interesting framework for applications that need to be multiplatform, with no source code alteration. It's economic in terms of space, my installer was just 2.6MB. And best of all you can fully test it in one side and be almost sure it will look and feel the same in another platform. Also Wimp is a very good solution for disguising (integrating) you Gtk based application on win32 platform.
Pitfalls:
- Find an updated Gtk2 runtime with libglade already bundled. GladeWin32 project is nice, but you have to trim it down if you're concern with space
- Correct the
MS SansvsSanson Wimp - Importing the right modules to cx_Freeze work correctly, in my case:
gobject,atk,pango,gtk.glade,codecs,encodings.utf_8,os,sys - Correct the
pango.modulesfile, because the generated bypango-querymoduleswasn't suited for distribution
Created resources:
- gtkrc - corrects 'MS Sans' problem.
- installer.nsi - used to generate the installer
- pango.modules - auto generated and correct to represent relative and expanded path.
- makedist.bat - script used to compile the project into a standalone executable
Used resources: