Python GUI(tkinter; pygtk+glade), py2exe - python

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.

Related

GUI development for my c application . How to proceed?

I have developed an application in C which works on command line interface. I want to make my code display data over GUI, and I would also want to provide a window waiting for user input.
I need some directions on how to proceed. I came across a few sites saying libraries in python will serve the purpose.: WxPython, TKinter, PyQt
How to start abt? How do I link my C application to the GUI? And should I develop a GUI application, too, and then link it to my C application?
I am a beginner. Please help.
Thanks.
I recommend using GTK+ or Qt for your application.
They are popular, cross-platform and both have extensive documentation and great community.
Take a look at Cython for easily connecting the C world with the Python world if you decide to link your C code with the UI, it's highly convenient as a glue language.
Depending on your application, it might be a good option to keep it as a command line application that you call from a python program. The subprocess module is the usual route here, but there are some tools that can help even more -- like commandwrapper.
As for the GUI, apart from the already mentioned (perfectly viable) alternatives in other answers, Kivy is an interesting newcomer, and pyFLTK is a lightweight, easy-to-learn, old-school approach. These are a bit exotic, but it's good to know they exist.
Finally, QT5 was recently released and it shows a lot of promise. You might want to consider it if you don't mind writing C++, it's various language wrappers are not ready yet.
You'll need to install the libraries for whichever GUI framework you want to use. WxWidgets is available for C, as well as Python, so you don't really need to link to the Python library. Other options (as indicated by Intelis) are GTK+ and QT.
If you're developing in Windows, then you may also use the Windows API to create your GUI application. Alternatively, you might consider using C++ (Either managed C++ or MFC).
Once you have installed the appropriate libraries, you will need to include them in your C source by using the #include <...> directive.
Take a look at these sites:
WxWidgets Tutorial.
GTK+ Tutorials
QT Tutorial

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.

Python - Best GUI library for the job?

I've been using WxPython and I've tried Tk, but it seems that, while both are good and I'll likely use them for other projects, neither of those appear to be capable of accomplishing the things that I want for my current project (which is fine, they're good at what they do).
Basically what I'm looking for is something that will allow me to make rich graphical GUIs. My specific goal is a window that will draw bitmap buttons, resize the parent window automatically to fit them, and possibly animate the resize with a slide effect and have the buttons fade in. Also being able to have my own window border style instead of the inbuilt one is important to me.
This particular project will be Windows only, so non-portable libraries are fine in this case, though portable ones would be great too.
If I missed how this can be done in either WxPython or Tk, I'm all ears.
PySide: http://www.pyside.org/
The PySide project provides
LGPL-licensed Python bindings for the
Qt cross-platform application and UI
framework. PySide Qt bindings allow
both free open source and proprietary
software development and ultimately
aim to support all of the platforms as
Qt itself.
The Windows version of PySide is quite new and may be considered as a beta version. PySide is API compatible with PyQt.
How about PyQt?
http://www.riverbankcomputing.co.uk/software/pyqt/intro
Just sharing my opinion: Kivy.
Innovative open-source library. Supports both 2.x and 3.x versions of Python.
Kivy - Open source Python library for rapid development of applications
that make use of innovative user interfaces, such as multi-touch apps.
Kivy is based on OpenGL ES 2 and includes native multi-touch for each platform and Android/iOS. It’s an event-driven framework based around a main loop, and is thus also suitable for game development.
Try Pyglet. Its a library for python that makes using OpenGL very easy. You can draw pretty good 2d interfaces using Quads.
I can't tell you what is best because that is subjective but I can give you another option: PyGTK
PyGTK lets you to easily create programs with a graphical user interface using the Python programming language. The underlying GTK+ library provides all kind of visual elements and utilities for it and, if needed, you can develop full featured applications for the GNOME Desktop.
PyGTK applications are truly multiplatform and they're able to run, unmodified, on Linux, Windows, MacOS X and other platforms.

Why are all the tk examples in a Python distribution written in TCL?

Now don't get me wrong, I'm not exactly a Python fan, but when you see a Tk directory inside of the python directory you kinda expect... Well Python. And yeah, I get that Tk came from TCL, but if I had to write a TCL to use Tk, I'd forget TK existed and use a completely different tool box. (The popularity of this combination completely eludes me.)
Expecting to see a relatively readable language like Python, and finding TCL is like walking in on your grandma naked. It's just visually painful.
I haven't drank the cool-aid when it comes to Python, but I use for simple task that I don't want to bother with C or C++ on and maybe if I want some for quick and dirty text processing. It just seems like a cruel joke to put TCL examples in the Python distribution.
Is there an equivalent package that includes those examples written in Python?
Edit:
I guess this also kinda begs the question is Tk the best option for GUI dev in Python?
There are no Tcl examples in Python's official distribution; whatever distro you're using must have bundled them on its own volition.
IMHO, Tk's only real advantage by now is the convenience that comes from having it bundled with Python. I was criticized for covering it in "Python in a Nutshell", but I stand by that decision because it is still "the" bundled toolkit, after all. But if you want something better and don't mind taking a tiny amount of inconvenience to procure it (and possibly to bundle it with apps you distribute), there are other excellent choices.
PyQt (if you can stand the GPL license or pay for the commercial one) and wxPython are IMHO currently superior offerings for cross-platform GUI apps (though you'll have to work to bundle them with py2exe or PyInstaller if you want to distribute a stand-alone app) and other packages are excellent if you don't care about cross-platform distribution or have specialized needs (e.g. pyui -- while now a general-purpose UI toolkit -- for simple UIs for games if you're using PyGame or PyOpenGL anyway).
Perhaps you should start by looking at the Python Tkinter documentation here and the Tkinter wiki here.
And where are you seeing Tcl examples? Are you looking at a Tcl library supplied with Python perhaps?
TCL / TK is a simple language to understand with which the applications are made very simple, very quickly occupying very few resources and hardware platform to be run on all platforms.
In TCL / TK exist there is also starkits or freewrap allowing wrap libraries, code and other files into a single package easier to carry around, to other platforms and run and install, without being necessary to bring all the libraries brings language.
While the evaluator of TCL / TK requires 3MB, the python requires much more, and uses many more that unused libraries.
When these applications grow, it is logical to migrate first to python and share the GUI using Tkinter, then they can move on to other GUI as QT or Wxwidgets, to work faster.
Who want to program with Tkinter, you must know how it works TCL and TK GUI with which are made tkinter.
Today is not here another library module GUI that there be a versatile as tkinter into a single file that allows cross-platform applications remain to be small and modular for all basic python project.
While tkinter is 2 MB, wxwidget are 80 MB and if you take a calculator, a notepad or a drawing application, you'll want a small GUI easy to incorporate into your projects.
Besides that today made Tkinter applications run faster and better in small devices with limited hardware, ARM or raspberry o smartphones but wxwidgets becomes slow.
If your application is basic and small and you platform is not robust chooses Tkinter, but if is big and heavy and platform of very much resources chooses wxwidget o QT.

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