User:FT2/Development

This section covers my development work for the wiki. I'm teaching myself to code, to add a few tools that would be useful.

Framework
I've adopted Python + Qt as my platform, and considering I'd barely heard of the latter a week ago here's the reasons:


 * Qt is a full GUI development environment. You can create forms and windows to a precision and degree of options that proprietary development development environments can't seem to touch (MS Visual Studio for example). Options for absolutely everything, and pretty much full everything. Also interface translation's built in too (important for wiki) and it's completely cross platform with a low footprint (Windows, Linux, mac etc).


 * Python is a basic-like intuitive language, powerful and simple. A number of wiki-developers use Python, including "pywikipedia", a framework for interacing to Wikipedia itself, which is actively developed. That means other wiki developers will be able to improve and fork any work I do, or take it over in future. Python is stable, and also has a mature interface with Qt (PyQt).

It took quite a while to get these working properly; I'm documenting the "howto" below.

How to get Python and Qt working together
The following quick guide is for Windows; Linux and Mac are probably similar.

You'll need to be sure you're using the same major versions of pythony things - right now that means I'm using Python 2.6.x (not 3.1.x), and Qt 4.5+.

Qt and Python
QT's native language is C++. Python can work with Qt, but Qt can't handle python in full, natively. So you have to add some extra software, and also a few things will need coding and debugging outside Qt (the Qt debugger does C++ only, and won't be helpful).

Software
You need quite a few things, but they're simple to get.


 * 1. Download and install Qt (LGPL, "Qt framework only", all 170+ MB of it). It's used for GUI form design ("Qt creator" or "QT designer"). This one's optional -- PyQt contains a stripped down version of this if you prefer, but the full version is compatible, and has some extra options and tools that are useful. Plus cutting edge is nice...


 * 2. Download and install Python itself. Again, the right version.


 * 3. Download and install PyQt (get the right version). It's only 18 MB long. It contains a stripped down version of Qt's GUI but we can ignore that. More to the point PyQt contains code and libraries for translating Qt ".ui" form files into Python and running them in python. That's what we really want :) Install AFTER python, not before! Otherwise it can't integrate properly with python.


 * 4. If using Windows, download PyWin32, a windows specific Python thingy that apparently is needed on Windows. You might also need "Microsoft Visual C++ 2008 Redistributable" (vcredist_x86.exe) too if you don't already have it. (If Mac or Linux need anything, I'm unaware of it but will check later)


 * 5. If you plan to compile your python code, grab Py2exe as well. It might need msvcp90.dll to run, which is another Microsoft Visual C++ dll; if so place this in the C:\Program Files\Python\Dll folder. Technically Py2exe doesn't compile Python; it collects all the code and DLL's needed to run it on another PC and creates a single file or folder with them (which can be unzipped and run or packaged with an installer), but it's good enough for distributing your code to other users. Similar programs exist for program distribution on other platforms (Py2App for Mac, etc).


 * 6. Run Qt Creator (or PyQt Designer), and go play. Create a random window. Don't try adding backing code yet; just have fun designing GUI's and look at the eye candy and all the pretty options! :)

Overall right now (early 2010; this might change) I'm using:
 * qt-sdk-win-opensource-2009.03.1.exe (Qt: 180 MB)
 * python-2.6.4.msi (Python itself: 14 MB)
 * PyQt-Py2.6-gpl-4.7-1.exe (PyQt: 18 MB)
 * pywin32-214.win32-py2.6.exe (PyWin32: 6 MB)
 * py2exe-0.6.9.win32-py2.6.exe (Py2exe: 200 KB)

and if missing:
 * vcredist_x86.exe (Microsoft Visual C++ redistributable: 2 MB)

and possibly (for py2exe):
 * msvcp90.exe (Microsoft Visual C++ redistributable??: 550 KB)

Notice all the "Python" stuff is the same basic version, 2.6.x in this case. Python 2.7 and Python 3 (AKA Python 3000) is due for release in 2010; when it's released you should probably use the equivalent programs for Py3 rather than stick with those for 2.x.

When you update these packages, it seems that it's best to uninstall and reinstall the python-related packages, since otherwise some of them may not work properly after updating.

File types

 * .PRO - header for a Qt project file
 * .UI - QT's internal saved code for a form
 * .H, .CPP - QT's C based stuff (ignorable)
 * .PY - Python code. Can be edited in Pythonwin or any unicode text editor, or run in Python or from Pythonwin.
 * .PYW - Python code again, identical to (and interchangeable with) the above but will open in a window not a console. If the code is a pure GUI application, edit the name from .PY to .PYW.
 * .PYC, .PYO - compiled Python bytecode

Extra config, and tips
This stuff allows you to develop python in Qt productively.


 * 1) Add the python program folder to PATH in the system control panel (ie "c:\...\python") so that python.exe can be found easily.
 * 2) Put the Microsoft DLL's in the folder shown, (if needed)
 * 3) The installed file pyuic4.bat may need editing to ensure it points to python.exe (known bug in PyQt)
 * 4) Copy and save a few standard files (link), being careful to save them in unicode format:
 * 5) * An updated version of the UI-to-Py converter, called "UiToPy.bat"
 * 6) * The header file for opening and using UI generated code in Python and example files of other basic Py and PyQt code tasks.
 * 7) Create a few standard context menu entries in the registry, if using Windows:
 * 8) * "Compile to Python" for file type .UI
 * 9) * "Open in Qt" or "Open in PyQt" for .PRO files (Qt project headers)
 * 10) Hints and tips:
 * 11) * Pythonwin seems to cache imported files; if you edit a formand recompile it, exit and re-enter Pythonwin to ensure it's using the latest version of the .PY code.
 * 12) * Python is unicode, NOTEPAD isn't. Saving python code in a DOS format text file may not work. Use Pythonwin or a unicode-capable text editor.

Example code (out of date)
UiToPy.bat REM Syntax is: UiToPy REM no path or drive needed, just execute it in the folder where the .iu definitions are

"C:\Program Files...\Python\Lib\site-packages\PyQt4\pyuic4.bat" -d "%~dpnx1" > "%~dpn1.py"

Use this program to translate a Qt .ui (window definition) file into executable Python code. It automates the creation of ".py" files (python) from ".ui" files (definition data for forms in Qt). Replace "program files..." by the path to your \Python/Lib folder.

If you run this from a command prompt, you'll see any debug info and errors.


 * 4. Create the following file in PythonWin, called "RUNME.pyw":

import sys from PyQt4 import QtCore, QtGui from myproject import *

class StartQT4(QtGui.QMainWindow): def __init__(self, parent=None): QtGui.QWidget.__init__(self, parent) self.ui = Ui_myform self.ui.setupUi(self)

if __name__ == "__main__": app = QtGui.QApplication(sys.argv) myapp = StartQT4 myapp.show sys.exit(app.exec_)

This is the fairly generic python wrapper to allow python GUI based code to be run. Replace "myproject" with the name of the ".py" file that contains the form (don't add ".py" to the end of it!). Replace "myform" by the name of the form in that project which is the "startup form".

Double click this file to execute the .py code from the previous step as a GUI.

Again, if you run it from a command prompt ("python.exe RUNME.py[w]") you'll see any debug errors.

Being productive

 * 1. You will want to create classes and actions, not just GUI windows. Use "create forms + classes" in Qt, not just "create forms" to ensure the ".h" header files are also added for each form.

Compiling python to an executable
Whether literally compiled, or just processed to an interpreted bytecode, a number of packages exist that allow you to create a .exe or .rpm (or Mac equivalent) and associated files from stuff you create.

Hints and tips

 * Windows and Python use different text file formats - DOS and Unicode or something. I found that editing .py or .pyw files was best done via pasting into PythonWin's editor and saving from there. Normal text editors could mess them up.


 * ".py" files are python code (text file format). Renaming your main file from ".py" to ".pyw" means it'll open directly in a window, not in a command prompt at first.