Python, PyQt4 and Rapid Prototyping

Beware!

It's dangerous to use prototyping professionally. Sometimes management folks tend to "not realize" the difference between a well done prototype and the result they desire for real. - Therefore use GPL licensed stuff "for compliance reasons" - if you know what I mean. Saves job and time.
There're numerous ways to enhance the following examples. Feel free to do so.

I recently used the new (GPL licensed version) of the Qt Creator IDE in order to build a Qt GUI. I converted it into a Python source file. Afterwards I used py2exe to deploy my prototype binary to Windows. If you like that idea... here's a very short walk-through. It's short because it's "rapid" ;). In general I think this is interesting to publish PoC tools, to test things and to demonstrate ideas. It's definitely not for long-term projects and large teams.

Get the development environment

If you're new into Python and Qt stuff consider reading Rapid GUI programming with Python and Qt by Mark Summerfield. It's a good start-point, but also covers advanced techniques in later chapters. I have to say that my PyQt4.uic.loadUi is considered to be uncommon.

You need:

  • Python >= 2.5
  • Qt4, PyQt4 and all dependencies (sip e. g.)
  • a Windows machine for py2exe (last section)

What you get:

  • a portable cross-plattform one-codebase prototype (yes, it would work without the exe-fication on a development Windows machine)
  • maybe you want to use Qt for Java instead of AWT or Swing? It's an OpenSource community project now
  • native looking interfaces
  • looks like you did a lot of work ;)

Punch it, Chewie!

Open Qt Creator. Create a GUI with a single button.

  1. Create a dialog with buttons form
  2. save it as foo_dialog.ui e. g.
  3. put it into the same path where your foo.py resigns

Looks like that:

Bild 1.JPG
Just the Mac version as an example.

You can open the ui-file with any text-editor any you'll realize that it's just some XML-like stuff in there. And this is not VisualStudio. You can edit the values manually without destroying the project.

  1. import sys
  2. from PyQt4 import QtGui, uic
  3.  
  4. app = QtGui.QApplication(sys.argv)
  5. window = uic.loadUi("foo_dialog.ui")
  6. window.show()
  7.  
  8. sys.exit(app.exec_())

That's it Chewie! - It doesn't do anything right now. It simply loads the GUI form. I have called this button foobarButton within Qt Designer.

If you don't like to load the ui-file directly into your program, you can use pyuic4 from PyQt4 (pyuic.py -o foo_dialog.py foo_dialog.ui). That's better, and take a look at the -x option which add the __init__.

  1. import sys
  2. from PyQt4 import QtGui, QtCore
  3. # import foo_bar # same dir here
  4. from foo_bar import Ui_Dialog
  5.  
  6. class TutApp(QtGui.QMainWindow):
  7.     def __init__(self):
  8.         QtGui.QMainWindow.__init__(self)
  9.         self.ui = Ui_Dialog()
  10.         self.ui.setupUi(self)
  11.         self.connect(self.ui.foobarButton, QtCore.SIGNAL("clicked()"), message)
  12.        
  13. def message():
  14.     # print "That's it Chewie!\n"
  15.     sys.exit(app.exec_())
  16.    
  17. if __name__=="__main__":
  18.     app = QtGui.QApplication(sys.argv)
  19.     window = TutApp()
  20.     window.show()
  21.     sys.exit(app.exec_())

It's very simple. The only thing to understand is, that QtCore handles signals (if you're in VS read "events") and needs to pass them to handler functions like message().

Surely often you might want some Drag & Drop actions, or tabs.

edit: drag & drop for python, not C++

The simplicity of these examples shouldn't make you think that Qt in conjunction with Python lacks features. - It's very powerful, but also structured and modular. Qt even targets Symbian and most Symbian apps don't have much more features than shown here ;).

You now can:

  1. create cross-plattform GUIs, that are separated from the program backends
  2. rapidly add functions to GUI-ficated Python programs
  3. deploy them everywhere

What you can't is to reach the average Windows user. That's possible too:

py2exe and Qt4

If you were able to install PyQt4 on your MacOS you're more than capable enough to install py2exe with Qt4 in a Windows development environment. That's just a single installer. Furthermore you need py2exe. Again a simple installer.

Thing is, that your XML GUI files can end up being zipped by py2exe - so your app won't start. In order to change this you can either disable the compression (for small stuff) or exclude those files necessary.

  1. from distutils.core import setup
  2. import py2exe
  3.  
  4. setup(name="foobar",
  5.       version="0.1",
  6.       author="ww",
  7.       author_email="wishinet@gmail.com",
  8.       url="crazylazy.info",
  9.       license="GNU General Public License (GPL)",
  10.       # !        
  11.           windows=[{"script": "the_snippet_from_above.py"}],
  12.       options={"py2exe": {"skip_archive": True, "includes": ["sip"]}})

That should be it. And yes: it may not work like that... but for a simple prototype again it does. Example use-cases are personal admin-tools, anything that automates work, file conversion utilities with Drag & Drop actions... And it's just one implementation, that looks native on every supported system.

Have fun,
wishi

Post new comment

The content of this field is kept private and will not be shown publicly.

Ihr Browser versucht gerade eine Seite aus dem sogenannten Internet auszudrucken. Das Internet ist ein weltweites Netzwerk von Computern, das den Menschen ganz neue Möglichkeiten der Kommunikation bietet.

Da Politiker im Regelfall von neuen Dingen nichts verstehen, halten wir es für notwendig, sie davor zu schützen. Dies ist im beidseitigen Interesse, da unnötige Angstzustände bei Ihnen verhindert werden, ebenso wie es uns vor profilierungs- und machtsüchtigen Politikern schützt.

Sollten Sie der Meinung sein, dass Sie diese Internetseite dennoch sehen sollten, so können Sie jederzeit durch normalen Gebrauch eines Internetbrowsers darauf zugreifen. Dazu sind aber minimale Computerkenntnisse erforderlich. Sollten Sie diese nicht haben, vergessen Sie einfach dieses Internet und lassen uns in Ruhe.

Die Umgehung dieser Ausdrucksperre ist nach §95a UrhG verboten.

Mehr Informationen unter www.politiker-stopp.de.