views:

5799

answers:

10

Python works on multiple platforms and can be used for desktop and web applications, thus I conclude that there is some way to compile it into an executable for Mac, Windows and Linux.

The problem being I have no idea where to start or how to write a GUI with it, can anybody shed some light on this and point me in the right direction please?

+73  A: 

First you will need some GUI library with Python bindings and then (if you want) some program that will convert your python scripts into standalone executables.

Cross-platform GUI libraries with Python bindings (Windows, Linux, Mac)

Of course, there are many, but the most popular that I've seen in wild are:

  • Tkinter - based on Tk GUI toolkit (de-facto standard GUI library for python, free for commercial projects)
  • WxPython - based on WxWidgets (very popular, free for commercial projects)
  • PyQt - based on Qt (also very popular and more stable than WxWidgets but costly license for commercial projects)

Complete list is at http://wiki.python.org/moin/GuiProgramming

Single executable (Windows)

Single executable (Linux)

  • Freeze - works the same way like py2exe but targets Linux platform

Single executable (Mac)

  • py2app - again, works like py2exe but targets Mac OS
lubos hasko
Talk about pyinstaller for windows, instead of py2exe.
changelog
It's worth noting that as of 4.5 QT will be under the LGPL.
CTT
...but PyQt is still GPL.
Nicholas Riley
+2  A: 

You don't need to compile python for Mac/Windows/Linux. It is an interpreted language, so you simply need to have the Python interpreter installed on the system of your choice (it is available for all three platforms).

As for a GUI library that works cross platform, Python's Tk/Tcl widget library works very well, and I believe is sufficiently cross platform.

Tkinter is the python interface to Tk/Tcl

From the python project webpage:

Tkinter is not the only GuiProgramming toolkit for Python. It is however the most commonly used one, and almost the only one that is portable between Unix, Mac and Windows

Justin Standard
So how would you make an installation package to deploy on systems of people who just want a 1-step install (e.g. double-click on a single installation package in Windows)?
Craig McQueen
For that there are a few approaches I know of: you would likely write an installer in batch script or compiled for the target system which would drop the needed files someplace and install the needed libraries and runtimes in the process.
Justin Standard
Installing Python as part of your own install would be a little fiddly, because you would have to deal with the possibility of the wrong version of Python already being installed. You'd have to install side by side, such that your application could access the newly-installed version of Python, but any existing applications continued to use the old version of Python. (ie without messing up things like the .py file association or the PATH)
Tartley
More likely your installer would check for the existence of the right version of Python, and if it found it, use that, otherwise install python as a part of the install process.
Justin Standard
Right - but that second option 'otherwise install python as a part of the install process', is exactly what I'm talking about. That would be fiddly, for the reasons I gave.
Tartley
A: 

Since python is installed on nearly every non-Windows OS by default now, the only thing you really need to make sure of is that all of the non-standard libraries you use are installed.

Having said that, it is possible to build executables that include the python interpreter, and any libraries you use. This is likely to create a large executable, however.

MacOS X even includes support in the Xcode IDE for creating full standalone GUI apps. These can be run by any user running OS X.

Matthew Schinckel
+8  A: 

An alternative tool to py2exe is bbfreeze which generates executables for windows and linux. It's newer than py2exe and handles eggs quite well. I've found it magically works better without configuration for a wide variety of applications.

Michael Twomey
+1  A: 

I'm not sure that this is the best way to do it, but when I'm deploying Ruby GUI apps (not Python, but has the same "problem" as far as .exe's are concerned) on Windows, I just write a short launcher in C# that calls on my main script. It compiles to an executable, and I then have an application executable.

Brian Warshaw
hmm, good but what if Ruby run-time is not installed?
Kugel
For these apps, none of my users are anonymous. I know if they have the runtime, and if they don't, I can install it for them. The principal concern in this topic is how to launch these applications without having to open up a command prompt and type the runtime's executable followed by the program name.
Brian Warshaw
+1  A: 

@Brian

Have you seen _why's technique for self-executing Shoes apps? It could probably be done with non-Shoes programs.

Adam Lassek
+1  A: 

For the GUI itself:

PyQT is pretty much the reference.

Another way to develop a rapid user interface is to write a web app, have it run locally and display the app in the browser.

Plus, if you go for the Tkinter option suggested by lubos hasko you may want to try portablepy to have your app run on Windows environment without Python.

poulejapon
+18  A: 

Another system (not mentioned in the accepted answer yet) is PyInstaller, which worked for a PyQt project of mine when py2exe would not. I found it easier to use.

http://pyinstaller.python-hosting.com/

Pyinstaller is based on Gordon McMillan's Python Installer. Which is no longer available.

Jamie
+1  A: 

While Qt's API is probably cleaner than wx, it does have the license problem. What if you want to use your code for work ? Qt doesn't allow that without the payment. Besides, the wx API is mature and works well on all platforms. The Python interface (wxPython) is well-supported and wraps the library tightly, allowing you to consult the wxWidgest documentation in your Python scripts. There's also a great book - "wxPython in action".

I've succeessfully used wxPython in conjunction with py2exe for creating stand-alone executables that look very professional and are fast and responsive.

Edit: These days Qt is no longer GPL only, so the license problem is gone.

Eli Bendersky
+3  A: 

There's also PyGTK, which is basically a Python wrapper for the Gnome Toolkit. I've found it easier to wrap my mind around than Tkinter, coming from pretty much no knowledge of GUI programming previously. It works pretty well and has some good tutorials. Unfortunately there isn't an installer for Python 2.6 for Windows yet, and may not be for a while.

Tofystedeth