Create a directly-executable cross-platform GUI app using Python - python

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?

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 (popular, free for commercial projects)
Qt using the PyQt bindings or Qt for Python. The former is not free for commercial projects. The latter is less mature, but can be used for free.
Complete list is at http://wiki.python.org/moin/GuiProgramming
Single executable (all platforms)
PyInstaller - the most active(Could also be used with PyQt)
fbs - if you chose Qt above
Single executable (Windows)
py2exe - used to be the most popular
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

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://www.pyinstaller.org/
Pyinstaller is based on Gordon McMillan's Python Installer. Which is no longer available.

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.

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.

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.

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.

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.

PySimpleGUI wraps tkinter and works on Python 3 and 2.7. It also runs on Qt, WxPython and in a web browser, using the same source code for all platforms.
You can make custom GUIs that utilize all of the same widgets that you find in tkinter (sliders, checkboxes, radio buttons, ...). The code tends to be very compact and readable.
#!/usr/bin/env python
import sys
if sys.version_info[0] >= 3:
import PySimpleGUI as sg
else:
import PySimpleGUI27 as sg
layout = [[ sg.Text('My Window') ],
[ sg.Button('OK')]]
window = sg.Window('My window').Layout(layout)
button, value = window.Read()
As explained in the PySimpleGUI Documentation, to build the .EXE file you run:
pyinstaller -wF MyGUIProgram.py

!!! KIVY !!!
I was amazed seeing that no one mentioned Kivy!!!
I have once done a project using Tkinter, although they do advocate that it has improved a lot, it still gives me a feel of windows 98, so I switched to Kivy.
I have been following a tutorial series if it helps...
Just to give an idea of how kivy looks, see this (The project I am working on):
And I have been working on it for barely a week now !
The benefits for Kivy you ask? Check this
The reason why I chose this is, its look and that it can be used in mobile as well.

# I'd use tkinter for python 3
import tkinter
tk = tkinter.Tk()
tk.geometry("400x300+500+300")
l = Label(tk,text="")
l.pack()
e = Entry(tk)
e.pack()
def click():
e['text'] = 'You clicked the button'
b = Button(tk,text="Click me",command=click)
b.pack()
tk.mainloop()
# After this I would you py2exe
# search for the use of this module on stakoverflow
# otherwise I could edit this to let you know how to do it
py2exe
Then you should use py2exe, for example, to bring in one folder all the files needed to run the app, even if the user has not python on his pc (I am talking of windows... for the apple os there is no need of an executable file, I think, as it come with python in it without any need of installing it.
Create this file
Create a setup.py
with this code:
from distutils.core import setup
import py2exe
setup(console=['l4h.py'])
save it in a folder
Put your program in the same folder of setup.py
put in this folder the program you want to make it distribuitable:
es: l4h.py
ps: change the name of the file (from l4h to anything you want, that is an example)
Run cmd from that folder (on the folder, right click + shift and choose start cmd here)
write in cmd:>python setup.py py2exe
in the dist folder there are all the files you need
you can zip it and distribute it
Pyinstaller
Install it from cmd
**
pip install pyinstaller
**
Run it from the cmd from the folder where the file is
**
pyinstaller file.py
**
Update
Read this post to make an exe on windows with pyinstaller the proper way and with one file and images in it https://pythonprogramming.altervista.org/auto-py-to-exe-only-one-file-with-images-for-our-python-apps/

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

You can use appJar for basic GUI development.
from appJar import gui
num=1
def myfcn(btnName):
global num
num +=1
win.setLabel("mylabel", num)
win = gui('Test')
win.addButtons(["Set"], [myfcn])
win.addLabel("mylabel", "Press the Button")
win.go()
See documentation at appJar site.
Installation is made with pip install appjar from command line.

There's three things you could do:
The first thing is to find a GUI Designer that can launch its code as standalone applications like .exe files. I use a version of MatDeck (for people using GUI Designers I recommend MD Python Designer) as I believe(I use another version so I'm not too sure.) it allows me to convert the code to a standalone applications and by having it as such, there is no need to install the software on every PC that's going to run the program.
The second option is partially bypassing the problem, launch the GUI as a web page. This would give you the most compatibility as most if not all OS can utilize it. Once again, you would need a GUI Designer that can convert its components into a web compatible format, I've done it once and I used the same version of MatDeck(Visionary Deck), I would not recommend MD Python Designer this time as I don't know if it can turn its GUIs into websites using web assembly whereas Visionary Deck I've tried and tested. As with all things there are most likely other software this is just one I use frequently because I work a lot with Mathematics and Physics.
The third option is also kind of bypassing the problem but do it in Tkinter and just ensure you have a Python IDE or just plain old Python and run the code, this will launch the GUI. This is a good solution and maybe the simplest but I wouldn't class it as the shortest or the best. If you only plan to switch between a few operating systems and computers this will probably be your best bet.

Related

Is wxPython Needed on the End User's Computer

I am making programs that solve and show work for math problems. I would like to add a GUI, and I think wxPython will be best. If I use wxPython for the GUI, will the end user need wxPython on their computer in order to use the program with the GUI? If not, what would should I use?
These apps will be used on mostly Windows, but I would also like them to work on Macs and Linux. I'm not for sure if any Python GUI elements will work on Android through SL4A, but if you know any, that would be appreciated.
Also, I know Tkinter is bundled with Python, but is it a dying technique, as I have read?
Thanks!
There are tools for packaging a python program and its libraries into an executable that can run on its own. I keep this list handy:
http://www.freehackers.org/Packaging_a_python_program
I'm sure at least one of those tools will handle wxPython, because I did it a few years ago. (Sorry, but I don't remember which one.)
Yes, tkinter's popularity has been waning for years. See this question for some more options:
higher level Python GUI toolkit, e.g. pass dict for TreeView/Grid
If your software is mostly about the complicated processing, with a fairly simple UI, tkinter is probably fine
I am using cx_freeze for this without any problems. Worked for me on Windows and Linux.
Tkinter comes with Python, so it can be handier in some respects just because of that. On the other hand, wxPython uses the native widgets of the OS (which has it's own set of pros and cons). I personally prefer wxPython. But no, Tkinter is not dead to my knowledge.
You can use py2exe to bundle up your app on Windows or you could use cx_freeze or bb_freeze. There's also PyInstaller, which I think can create some kind of Linux bundle, but the docs are kind of confusing. For Mac, see py2app.
I'm not aware of any specific Python GUI toolkits for Android.
PyInstaller.
install and run.
cmd -> python pyinstaller.py NAMEOFSCRIPT.py --onefile --noconsole.
easy as 123.

How to create a Mac OS X app with Python?

I want to create a GUI application which should work on Windows and Mac. For this I've chosen Python.
The problem is on Mac OS X.
There are 2 tools to generate an ".app" for Mac: py2app and pyinstaller.
py2app is pretty good, but it adds the source code in the package. I
don't want to share the code with the final users.
Pyinstaller generates UNIX executable, so how to run it on Mac? I
created a bundles with this executable, but the resulted ".app" is
not working.
The questions are:
How to configure py2app to include the source code in the
executable, so the final users will not have access to my program?
How to convert UNIX executable to Mac ".app" ?
Is there a way to compile Python code with GCC ?
In Windows it's easy, I created an "exe" file from Python code and
it works. Is it possible to create a single file "app" for Mac ?
P.S. I use two computers (Windows and for Mac), Python 2.7, wxPython, py2exe, py2app and pyinstaller.
Also, I have checked out these sites:
http://svn.pythonmac.org/py2app/py2app/trunk/doc/index.html
http://www.pyinstaller.org/export/develop/project/doc/Manual.html?format=raw
http://www.pyinstaller.org/wiki/Features/MacOsCompatibility
http://www.stackoverflow.com/questions/2933/an-executable-python-app
How to configure py2app to include the source code in the executable,
so the final users will not have access to my program?
Unless you very seriously hack the python interpreter (and include the mangled version) there is no really good way to hide the source from a moderately skilled and determined user. I strongly believe this is true on Windows also. Basically, whether you include true source or bytecode, a pretty clean version of the source can be recovered. More importantly, in my opinion, unless you include the actual source code (as opposed to bytecode, you will introduce a possible dependency on the interpreter version).
How to convert UNIX executable to Mac ".app" ?
What do you mean by a UNIX executable? A Darwin (OS X) binary [which isn't actually UNIX]? That can be done using the kinds of tools you already mentioned, but it must be done carefully to avoid library dependencies.
If all you want it a simple wrapper to put a command-line binary into a window, it's pretty easy to accomplish and the free XCode suite has several examples that would serve (depending on what output
you wan to deliver, if any).
Is there a way to compile Python code with GCC ?
GCC does not compile Python. It's a different language (although there tools in the gcc family rthat support multiple language front-ends, but not Python). There are tools that attempt to translate Python into C, and then you can compile that into a true binary, but this only works for programs that avoid certain types of construct, and the process (and restrictions) need to apply your libraries as well.
One project to allow this is Cython. It works well for some types
of code, mostly numerical code, but it is not trivial to install and
exploit, very especially if you want to produce something that runs on multiple
different computers.
In Windows it's easy, I created an "exe" file from Python code and it
works. Is it possible to create a single file "app" for Mac ?
I would have to say I am skeptical -- very skeptical -- about this. Just like the OS X case, the exe almost certainly has the source code trivially accessible within it.
One fairly easy trick is to encrypt the source code and then decrypt it on the fly, but this
seems to me like more trouble than it's worth.
PyInstaller will automatically create bundles under Mac OSX for windowed executables. When running ypinstaller.py, make sure to pass the option "--windowed".
This feature is documented in the website of pyinstaller
If you're not completely committed to wxPython (and for anyone else looking for a cross platform Python GUI framework), I recommend you check out Kivy. It's cross platform, GPU accelerated, and it will do the app packaging for you. It's easy to jump into, has a well thought-out architecture, and gives you an incredible amount of flexibility in terms of the interface. It's the best way I've found to make a cross platform Python GUI app.
cxFreeze was the choice.
I use it pack my python program to a Mac OS X app. Which works like a charm.
Automator was already mentioned as a quick and simple solution for Pythons scripts that are contained in a single file, but since the Automator UI has so many options, and it is not obvious how to actually do it, I'll provide step-by-step instructions (verified to work on Yosemite):
In Automator select File > New and pick Application as document type.
Next, make sure Actions tab is selected on the left, and then in the search box type run. Among other options you'll see Run Shell Script — doubleclick it, and an editor window will appear in the right panel.
From the Shell dropdown menu select /usr/bin/python.
Paste your Python code into the edit window and then pick File > Save.
By default, the app will be saved under $HOME/Applications and will appear in Spotlight.
If you want to be able to set your own icon and have some fancy features, like task bar icons with a menu, log windows etc, then have a look at Platypus — an open-source app for creating MacOS native bundles.
2: You can't "convert" it, but you can move the executable to App.app/Contents/MacOS/something in a .app file, with CFBundleExecutable set to "something". This would not generally be recommended.
A motivated person could probably reconstruct usable source code from the Python bytecode in your app, so you might reconsider your opposition to py2app. If you don't trust your final users, why are you doing business with them?
Having used py2exe for windows users so they wouldn't have to deal with library versions, I've torn apart the compiled programs, they include the python bytecode files. While you can make it a violation of the license to look inside those, the fact is that if a computer can execute them, I can read them. It is possible to compile python programs with gcc, via a C preprocessor (try looking for 2c.py on google), I don't know if any of them support GCC. Again, you don't gain any security through using them, but you can get a significant speed improvement.
I haven't tried it with big Python projects, but for my own scripts, the easiest way I found was to use Automator
You can interactively create an app project with Run Shell Script action, then paste in your script in its editor, select your shell program (/usr/bin/python), finally save the project. And you have yourself a Mac native app.
Automator can also be driven by AppleScript. So you can pipeline this py-2-app conversion process to your build scripts.
I've never tested a GUI program with it so I don't know if you'll be happy with it. But I'd give it a try since you may wonder how well all the cited 3rd-party python modules/applications are maintained, and how long they are gonna last. Coming bundled with OS X, Automator will likely stay, unless Apple got REALLY tired of it.
cxFreeze is best solution available, first create your program or application using python and than make setup file for your application, and than build the app using build command python setup.py build, according to your requirement you need to make some changes.
The only way is py2app. You have no other way. Sorry.
The research you did seems very solid and you did not miss anything.

How do I compile a PyQt script (.py) to a single standalone executable file for windows (.exe) and/or linux?

I started to fiddle with PyQt, and made a "beautiful" script from the pyqt whitepaper example app (pastebin)
It works perfectly in Windows and Linux (with qt environment already installed on both).
Now my question is: Since I am trying to use Qt because it is compiled (at least pure old C++ based Qt), how can I compile some .exe file to run it on Windows, or a standalone executable for Linux.
The point is that I want the program to be compiled, because of speed and portability, instead of interpreted from source, which would require a previous setup on any machine. One of the goals, for example, is sending small gui scripts via email to coworkers who are not programmers at all.
if you want completelly create one stand alone executable, you can try PyInstaller . i feel it's better to create one stand alone executable than cx_freeze or py2exe (in my experience). and easy to use (full documentation available in the site).
It supports Python 3.6 or newer.
Pass the --onefile argument if you want to create completely standalone .exe. in example :
pyinstaller.exe --onefile --windowed app.py
After spending many weeks on this and trying all the alternatives - PyInstaller, py2exe, cx_freeze,... - I created my own library: https://build-system.fman.io/. It is based on PyInstaller but solves many of its common pain points. It also lets you create native installers on Windows, Mac and Linux.
You may want to check out cx_freeze. It claims to create executables which are "cross platform and should work on any platform that Python itself works on."
I came across it in exploring the moneyGuru package which uses PyQt. I downloaded the moneyguru.exe file to my Windows XP system, executed it, and it worked fine on Python 3.2.
You can clone the hg repo from here to see how it.s done.
There is a module named Py2EXE, which will do exactly what you want to do. It will convert the script into a .exe file to run on windows. I'm not sure about linux, but I bet there is a module out there somewhere.
py2exe.com
I am using pyinstaller
pip install pyinstaller
I don't know, but pyinstaller does't append sip.pyd. So, your need a PyQt5\sip.pyd. I recommend nice windows style qwindowvistastyle.dll.
Make build.cmd file as:
pyinstaller --onefile --clean ^
--add-binary="C:\Users\Quazer\.virtualenv\pyqt5-36\Lib\site-packages\PyQt5\sip.pyd;PyQt5" ^
--add-binary="C:\Users\Quazer\.virtualenv\pyqt5-36\Lib\site-packages\PyQt5\Qt\plugins\styles\qwindowsvistastyle.dll;PyQt5\Qt\plugins\styles" ^
.\main.py
^ - new line in command file (.cmd, .bat)
Since I am trying to use Qt because it is compiled
You're defeating this benefit by using Python. Although the other answers give an introduction to the options for distributing Python code without requiring users to install Python themselves, Python is intended to be an interpreted language so there will be downsides to each of these options (ex. speed, program size, compatibility, etc...). They may or may not be deal-breakers to you.
Your two other options are:
Embrace the interpreted nature of Python: have people you're sharing your program with install Python and the dependencies. You can simplify this process significantly though. Ex. on Linux, use a package manager.
Write your program in C++. Doing so would allow you to truly compile a single, native executable. This unfortunately means dropping Python, but there's reasons people still write code in less beautiful languages like C++ and it sounds like you might be running into some of them.

Python GUI(tkinter; pygtk+glade), py2exe

I'm new with python programming and GUI. I search on internet about GUI programming and see that there are a lot of ways to do this. I see that easiest way for GUI in python might be tkinter(which is included in Python, and it's just GUI library not GUI builder)? I also read a lot about GLADE+PyGTK(and XML format), what is there so special(glade is GUI builder)?
Can anyone make some "personal opinion" about this choices?
I have python code, I need to make simple GUI(2 button's-open-close-read-write,and some "print" work) and then make some .exe file (is there best choice py2exe=?). Is there a lot of changes in code to make GUI?
Many thanks
If your GUI is really that simple, you should go with the built-in tkinter.
There's a Hello, Tkinter tutorial that you can follow, it's pretty straightforward. Concerning the creation of executables, py2exe should work without problems in most cases (though I haven't tried with tkinter). Another way to create an executable is to add a special parameter to your "setup.py" file:
setup(...,
entry_points = {"gui_scripts" : ['name-of-executable = name_of_package.launcher:main']})
This would, for example, create an executable that can be run by typing "name-of-executable" into a terminal (even on Windows if Python's "scripts" path is in the PATH ^^). It runs the function "main" in the module called "name_of_package". That way, you don't have to use py2exe but can create a Windows installer, or a Debian package, for instance.
For more complex projects, I can absolutely recommend PyGTK with Glade as interface designer. It requires several Python packages to be installed, plus a GTK+ installation (which is not always that easy on Windows). The API is awesome, well-documented and Glade is very easy to use, once you get used to the layouting concepts of GTK. But my opinion is kind of biased because I've done multiple projects in PyGTK. wxWidgets or PyQT are good alternatives. For example, bazaar explorer is written using QT.
I really like PyQt bindings for Qt library.
What is PyQt?
Qt itself is a very nice framework - rich, powerfull, elegant (for my taste, at least). And PyQt does a very nice job of exposing that functionality to a scripting environment.
Plus, there is a very nice book about PyQt development - Rapid GUI Programming with Python and Qt - working through it helped me a lot.

Python GUI Application redistribution

I need to develop a small-medium sized desktop GUI application, preferably with Python as a language of choice because of time constraints.
What GUI library choices do I have which allow me to redistribute my application standalone, assuming that the users don't have a working Python installation and obviously don't have the GUI libraries I'm using either?
Also, how would I go about packaging everything up in binaries of reasonable size for each target OS? (my main targets are Windows and Mac OS X)
Addition:
I've been looking at WxPython, but I've found plenty of horror stories of packaging it with cx_freeze and getting 30mb+ binaries, and no real advice on how to actually do the packaging and how trust-worthy it is.
http://wiki.wxpython.org/CreatingStandaloneExecutables
It shouldn't be that large unless you have managed to include the debug build of wx.
I seem to rememebr about 4Mb for the python.dll and similair for wx.
This may help:
How can I make an EXE file from a Python program?
Python has an embedded GUI toolkit named TKinter which is based on Tk library from TCL programming language. It is very basic and does not have all the functionality you expect in Windows Forms or GTK for example but if you must have platform independent toolkit I see no other choice taking in mind that you also dont want to grow that much the binary.
Tkinter is not hard at all to use since it doesnt have millions of widgets/controls and options and is the default toolkit included in most python distributions, at least on Windows, OSX and Linux.
GTK and QT are prettier and more powerful but they have a one big disadvantage for you: they are heavy and deppend upon third libraries, especially GTK which has a lot of dependencies that makes it a little hard to distribute it embeded in your software.
As for the binary creation I know there is py2exe which converts python code to win32 executable code (.exe's) but im not sure if there is something similar for OSX. Are you worried because people could see the source code or just so you can bundle all in a single package? If you just want to bundle everything you dont need to create a standalone executable, you could easily create an installer:
Creating distributable in python
That's a guide on how to distribute your software when it's done.
http://Gajim.org for Windows uses python and PyGtk. You can check, how they did it. Also, there's PyQt for GUI (and wxpython mentioned earlier).
I don't have any experience building stand-alone apps for any platform other than Windows.
That said:
Tkinter: works fine with py2exe. Python Megawidgets (an "expansion library" for Tkinter) works fine also, but it does funky things with dynamic imports, so you need to combine all the components into a big file "pmw.py" and add it to your project (well, you'll also have pmwblt.py and pmwcolor.py). There are instructions for how to do this somewhere (either on py2exe wiki or in the PMW docs). Tix (an extension to Tk that you can use with Tkinter) doesn't work with py2exe, or at least that was my experience about four years ago.
wxPython also works fine with py2exe. I just checked an app I have; the whole distribution came to around 11MB. Most of that was the wx DLLs and .pyd files, but I can't see how you'd avoid that. If you are targetting Windows XP, you need to include a manifest in your setup.py or else it will look ugly. See this email for details.
I've used py2Exe myself - it's really easy (at least for small apps).
Combination that I am familiar with: wxPython, py2exe, upx
The key to resolving your last concern about the size of the distribution is using upx to compress the DLLs. It looks like they support MacOS executables. You will pay an initial decompression penalty when the DLLs are first loaded.

Categories