Wednesday, December 30, 2015

wxFormBuilder - Tutorial on “A GUI for making GUIs” for Python

Graphical User Interfaces (GUIs) in Python are commonly created using Tk via the Tkinter package. However, at the moment of writing this post, designing GUI via the Tkinter package is done purely in code (python syntax) which means that a simple dialog window may consist of approximately 100+ lines of code.
Wouldn’t it be pleasant if we had a visual tool for making GUIs? That is “A GUI for making GUIs”. Creating GUI with code (WxPython) is too tedious work and it requires lots of attention and time. With WXFormBuilder, you create GUI much faster and efficiently in less time. In most cases this is faster than writing this code by hand.

That is what ‘wxFormBuilder’ is set to realize. WxFormBuilder is a Rapid Application Development (RAD) tool for wxWidgets GUI design. It is an open source GUI designer application for wxWidgets toolkit, which allows creating cross-platform applications.

WxFormBuilder allows you to design your GUIs visually and save them in a wxFormBuilder Project File - *.fbp file (which is just an XML file listing every component in your GUI and defining each component’s properties). Then, the *.fbp file can be converted automatically into a Python *.py file ready to be imported into your Python program. It also serves as Source code generation other programming languages are supported such as: C++, PHP, Lua and XRC.

Tutorial objective:
I will guide you through the basics of wxFormBuilder as used with wxPython generated code. The goal is for you to build a usable First GUI in Python.

We are going to create a little application that will take selected text from the user and append/print the chosen text to a static text box that has “Hello…” on it.
The end result will be a GUI (as sketched below) that just outputs: Hello… World, or Hello… Africa, or Hello… Asia, or Hello… Europe, or Hello… America - on the displayed static text box.

We need to have the following packages installed:-
~ Python 2.7
~ wxpython
~ wxformbuilder 3.5-RC1
~ Pyinstaller to freeze the application (create an .exe on Windows or a .app on osx)
~ Text editor (Sublime Text, Notepad++, Pycharm or anything similar).

Combination of any versions will do as long as they are compatible with each other. I used Python 2.7.10 Anaconda 2.3.0 (64-bit), wxPython (msw-unicode), wxFormBuilder 3.5-RC1 (unicode) and Sublime Text 3. The prerequisite packages most be installed as above in the order mentioned and we should be good to go.