The graphical user interface, or GUI, as it is more often known, is one of the three essential pillars of every application, along with security and performance. Maintaining the proper balance between these three critical factors can assist the user in providing an intuitive and seamless user experience.
Python is a precise programming language due to its growing popularity, easy learning curve, and widespread application in the real world. The next obstacle for beginners, assuming they have a good handle on Python programming fundamentals, is creating the user interface. Fortunately, many Python frameworks specialize in assisting developers in creating stunning yet highly intuitive user interfaces for modern apps.
The user interface (GUI) is the sum of the aspects that contribute to the overall user experience of the application. Conventional input modalities, such as keyboards, mice, and touchscreens, can control these visual features. A Python GUI framework makes designing the user interface for the application significantly easier than coding it by hand in Python.
PyQt5
Qt is a collection of cross-platform C++ libraries that implement high-level APIs for interacting with various parts of current desktop and mobile systems. Location and positioning services, multimedia, NFC and Bluetooth connectivity, a Chromium-based web browser, and classic UI development.
PyQt5 is an extensive collection of Python bindings for Qt v5. It is implemented as more than 35 extension modules and allows Python to be used as an alternative application development language to C++ on all platforms supported, including iOS and Android.
PyQt5 can also be incorporated in C++-based programs to allow users to adjust or improve the functionality of those applications.
Installation
pip install PyQt5
Pip helps create and install the bindings from the sdist package, but Qt’s qmake tool must be in the PATH.
The code below helps to initialize it.
app = QApplication(sys.argv) widget = QWidget()
Text cannot be added to a window right away. It must include on a label.
A label is a type of widget that can display text or images. These lines establish a QLabel and set the text and location of the label (horizontal, vertical).
textLabel = QLabel(widget) textLabel.setText("Hello World!") textLabel.move(110,85)
import sys from PyQt5.QtWidgets import QApplication, QWidget, QLabel from PyQt5.QtGui import QIcon from PyQt5.QtCore import pyqtSlot def window(): app = QApplication(sys.argv) widget = QWidget() textLabel = QLabel(widget) textLabel.setText("Hello World!") textLabel.move(110,85) widget.setGeometry(50,50,320,200) widget.setWindowTitle("PyQt5 Example") widget.show() sys.exit(app.exec_()) if __name__ == '__main__': window()
When designing graphical user interfaces, the PyQt5 arsenal includes the outstanding QtGui and QtDesigner modules, which give a plethora of visual features that the developer may integrate with a simple drag and drop. Of course, the option of building these elements by code exists as well, allowing the user to create both small-scale and large-scale applications easily.
Tkinter
Tkinter is Python’s de-facto usual GUI (Graphical User Interface) module. On top of Tcl/Tk is a thin object-oriented layer.
The following is an excellent approach to test whether Tkinter support is operational.
Implementation
Step 1
In a shell on an X console, launch an interactive Python interpreter.
import _tkinter # with underscore, and lowercase 't'
If it works, advance to the next step.
If it fails with the error “No module called _tkinter,” the Python setup must be changed to include this element (which is an extension module implemented in C). DO NOTmodify Modules/Setup (it is out of date). Tcl and Tk may need to install (when using RPM, install the -devel RPMs), or the setup.py script may need to edit to point to the correct root where Tcl/Tk is installed. If the user installs Tcl/Tk in the default locations, simply re-running “make” should create the _tkinter extension.
If it fails with a dynamic linker error, see above (for Unix, look for a header/library file mismatch; for Windows, ensure the TCL/TK DLLs can be found).
Step 2
>>> import Tkinter # no underscore, uppercase 'T' for versions prior to V3.0 >>> import tkinter # no underscore, lowercase 't' for V3.0 and later
Proceed to step 3 if it works.
If it fails with “No module called Tkinter,” update Python set to include the directory containing Tkinter.py in its default module search path. The user most likely forgot to include TKPATH in the Modules/Setup file. Finding that directory and adding it to the PYTHONPATH environment variable would be temporary. It is the Python library directory’s subdirectory “lib-tk” (when using Python 1.4 or before, it is named “tkinter“).
Step 3
>>> Tkinter._test() # note underscore in _test and uppercase 'T' for versions prior to V3.0 >> tkinter._test() # note underscore in _test and lowercase 'T' for V3.0 and later
It should result in the appearance of a tiny window with two buttons. When the user clicks the “Quit” button, it disappears, and the command returns. If this works, the user is good to go. (When performing this test on Windows from Python in an MS-DOS console, the new window frequently appears *under* the console window. It can also happen when the user uses iTerm on Mac OS X. Set it aside or look for the Tk window in the Taskbar / Dock.)
Tkinter was developed to provide current developers with consistent access to the Tk GUI toolkit via Python bindings. Most of the visual elements we’re familiar with are called widgets in Tkinter’s environment, and each of these plugins offers a different level of customizability.
Tkinter is included in the most recent Python installers for all major operating systems and provides a slew of regularly used elements. Some of these visual components are as follows:
- Buttons: used for collecting user input
- Check buttons: used for making selections
- Labels: used for showing textual information
- File Dialogs: used for uploading or downloading files to/from the application.
- Canvas: gives a canvas for drawing/painting things like graphs and charts
Kivy
Kivy is an open-source GUI framework written in a mix of Python and Cython that allows the user to create some of the most intuitive user interfaces, including multi-touch applications that leverage Natural User Interface (NUI).
- Kivy is Cross-Platform, and most inputs, protocols, and devices are supported natively, including WM Touch, WM Pen, Mac OS X Trackpad and Magic Mouse, Mtdev, Linux Kernel HID, and TUIO. There is a multi-touch mouse simulator provided.
- The visuals engine is based on OpenGL ES 2, and it employs a modern and fast graphics pipeline.
- Kivy is entirely free to use, with an MIT license (beginning with 1.7.2) and LGPL 3 for older versions.
from kivy.app import App from kivy.uix.button import Button class TestApp(App): def build(self): return Button(text='Hello World')