GUI Programming in
Python
A gateway to create Python-MySQL projects
Ashok Sengupta - PGT CS
Introduction
Like many other popular programming languages Python too supports GUI
development.
Python provides several different options for writing GUI applications
TkInter – Python’s standard GUI library
PyQt5 – A specific installable library written in C++ for rapid GUI development
wxPython – A cross platform GUI toolkit for python
https://wiki.python.org/moin/GuiProgramming contains a detailed list
Ashok Sengupta - PGT CS
GUI Form Builders
Creating a good looking GUI by manual coding can be tedious. A visual GUI
designer tool is always handy.
Many GUI development IDEs targeted at wxPython are available. Following are
some of them:
wxFormBuilder - an open source, cross-platform WYSIWYG GUI builder that can
translate the wxWidget GUI design into C++, Python, PHP or XML format
wxGlade - a GUI designer written in Python with the popular GUI toolkit wxPython,
that helps you create wxWidgets/wxPython user interfaces
BoaConstructor - GPL A RAD GUI Building IDE for wxPython.
Ashok Sengupta - PGT CS
Working with wxFormBuilder - Installation
Installer for wxFormBuilder can be downloaded and installed from
http://sourceforge.net/projects/wxformbuilder/
Install the wxPython package – pip install –U wxPython. For additional notes
visit https://wxpython.org/pages/downloads/
Ashok Sengupta - PGT CS
Component Palette
Basic GUI application
Give a sensible name to
your project
Click Python in Code
Generation option
Object Tree
Designer Window
Ashok Sengupta - PGT CS
Add a Box Layout or
Adding a layout on the frame resizer to the frame
We can
Layout select
visible only Vertical or
in object Horizontal
tree
In a box resizer
Ashok Sengupta - PGT we
CS can dynamically add number of control elements horizontally or vertically in rows or
columns depending upon the boundaries of the container Frame.
1. Click Forms Palette
Creating the Basic Form
3. Give
sensible
names to
Frame and a
Title too.
2. Click Frame control
to place the Frame
Ashok Sengupta - PGT CS
Adding and configuring Controls
2. Give a name on
Label Property
1. Click Label control 3. Click Expand to fit
from common palette the label on frame
to place on the Frame
Ashok Sengupta - PGT CS 4. Add a Text Control
Similarly add one more label and a Text Control and give it a name
Adding buttons and their events
2. Click the Events Tab
and give an
appropriate name for
the OnButtonClick
Event
1. Add a Button under
another box resizer
3. Similarly add
with wxHORIZONTAL
another button with
orientation and give it
name caption and
a name and
event as subtraction
label(caption)
Ashok Sengupta - PGT CS
Giving Final touch
1. Added another label and a Text
Field (txtRes) to display the result
of the operations
Ashok Sengupta - PGT CS
Generating Python Guard Code for GUI
• Now press F8 to generate the Guard python code
that will be saved in the same location where the
wxFormBuilder file .fbp is saved. The default
name for the file is noname.py.
• Rename the noname.py to calculatorwx.py and
please do not edit that file
• Create a python black file and name it as calc.py
in the same folder. We shall code it further to
apply our business logic.
Ashok Sengupta - PGT CS
The final Guard Code
Please do not Edit this Guard Code. We are going to inherit this
python class CalcFrame in Calculator class to be defined in
Ashok Sengupta - PGT CS
another file calc.py . Also we are going the to override the
event methods addition and subtraction in the Calculator class.
Writing code for the calc.py
Ashok Sengupta - PGT CS
Running the Application – Save and execute the
calc.py file to get the desired application running
Ashok Sengupta - PGT CS
References
https://wxpython.org/
https://www.tutorialspoint.com/wxpython/
Ashok Sengupta - PGT CS