In this article, we will go through the top 5 Python GUI libraries that you can use in your projects. Keep reading to find out about them.
What is a GUI?
A GUI or a graphical user interface is an interactive environment to take responses from users on various situations such as forms, documents, tests, etc. It provides the user with a good interactive screen than a traditional Command Line Interface (CLI).
List of Best Python GUI Libraries
Let’s get right into it and look at the top GUI Libraries for Python.
1. PyQT5
PyQT5 is a graphical user interface (GUI) framework for Python. It is very popular among developers and the GUI can be created by coding or a QT designer. A QT Development framework is a visual framework that allows drag and drop of widgets to build user interfaces.
It is a free, open source binding software and is implemented for cross platform application development framework. It is used on Windows, Mac, Android, Linux and Raspberry PI.
For the installation of PyQT5 , you can use the following command :
pip install pyqt5
A simple code is demonstrated here:
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.setGeometry(300, 300, 600, 400)
self.setWindowTitle("PyQt5 window")
self.show()
app = QApplication(sys.argv)
window = Window()
sys.exit(app.exec_())
The output of the above code is as follows:
ScienceSoft’s team of Python developers highlights the benefits of using PyQt:
PyQt is a mature set of Python bindings to Qt for cross-platform development of desktop apps. It offers a rich selection of built-in widgets and tools for custom widgets creation to shape sophisticated GUIs, as well as robust SQL database support to connect to and interact with databases.
2. Python Tkinter
Another GUI framework is called Tkinter. Tkinter is one of the most popular Python GUI libraries for developing desktop applications. It’s a combination of the TK and python standard GUI framework.
Tkinter provides diverse widgets such as labels, buttons, text boxes, checkboxes that are used in a graphical user interface application.
The button control widgets are used to display and develop applications while the canvas widget is used to draw shapes like lines, polygons, rectangles, etc. in the application. Furthermore, Tkinter is a built-in library for Python, so you don’t need to install it like other GUI framework. Given below is an example of coding using Tkinter.
from tkinter import *
class Root(Tk):
def __init__(self):
super(Root,self).__init__()
self.title("Python Tkinter")
self.minsize(500,400)
root = Root()
root.mainloop()
The output of the above code is as shown below:
3. PySide 2
The third Python GUI libraries that we are going to talk about is PySide2 or you can call it QT for python. Qt for Python offers the official Python bindings for Qt (PySide2), enabling the use of its APIs in Python applications, and a binding generator tool (Shiboken2) which can be used to expose C++ projects into Python.
Qt for Python is available under the LGPLv3/GPLv3 and the Qt commercial license.
So now let me show you the installation process and also an example. So for the installation, you can just simply use:
pip install PySide2
Here, is an example to set up GUI frame using PySide2.
from PySide2.QtWidgets import QtWidgets, QApplication
import sys
class Window(QtWidgets):
def __init__(self):
super().__init__()
self.setWindowTitle("Pyside2 Window")
self.setGeometry(300,300,500,400)
app = QApplication(sys.argv)
window=Window()
window.show()
app.exec_()
The output of the above code is as shown below:
4. Kivy
Another GUI framework that we are going to talk about is called Kivy. Kivy is an Open source Python library for the rapid development of applications that make use of innovative user interfaces, such as multi-touch apps.
Kivy runs on Linux, Windows, OS X, Android, iOS, and Raspberry Pi. You can run the same code on all supported platforms. It can natively use most inputs, protocols and devices including WM_Touch, WM_Pen, Mac OS X Trackpad and Magic Mouse, Mtdev, Linux Kernel HID.
Kivy is 100% free to use, under an MIT license.
The toolkit is professionally developed, backed, and used. You can use it in a commercial product. The framework is stable and has a well-documented API, plus a programming guide to help you get started.
The graphics engine of Kivy is built over OpenGL ES 2, using a modern and fast graphics pipeline.
The toolkit comes with more than 20 widgets, all highly extensible. Many parts are written in C using Cython, and tested with regression tests.
Coming to the installation of Kivy, you need to install the dependency “glew”. You can use the pip command as below:
pip install docutils pygments pypiwin32 kivy.deps.sdl2 kivy.deps.glew
Enter this command and hit enter, it will be installed. After that you need to type this command to install Kivy:
pip install Kivy
So after installation, let me show you a simple example of Kivy to show how easy it is.
from kivy.app import App
from kivy.uix.button import Button
class TestApp(App):
def build(self):
return Button(text= " Hello Kivy World ")
TestApp().run()
The output of the above code is as shown below:
5. wxPython
So the last GUI framework that we are going to talk about is wxPython. wxPython is a cross-platform GUI toolkit for the Python programming language.
It allows Python programmers to create programs with a robust, highly functional graphical user interface, simply and easily. It is implemented as a set of Python extension modules that wrap the GUI components of the popular wxWidgets cross-platform library, which is written in C++.
Like Python and wxWidgets, wxPython is Open Source.
wxPython is a cross-platform toolkit. This means that the same program will run on multiple platforms without modification. Currently, the Supported platforms are Microsoft Windows, Mac OS X and macOS, and Linux.
Now I’m going to show you the installation process and create a simple example. So for the installation just type the following command:
pip install wxPython
Here is an example:
import wx
class MyFrame(wx.Frame):
def __init__(self,parent,title):
super(MyFrame,self).__init__(parent,title=title,size=(400,300))
self.panel=MyPanel(self)
class MyPanel(wx.Panel):
def __init__(self,parent):
super(MyPanel,self).__init__(parent)
class MyApp(wx.App):
def OnInit(self):
self.frame=MyFrame(parent=None, title= "wxPython Window")
self.frame.show()
return True
app = MyApp()
app.MainLoop()
The output of the above code is as shown below:
Conclusion
So now we have seen 5 Python GUI libraries and in my opinion, PySide2 and pyQt5 are the more powerful GUI frameworks. But they do come with a commercial license and that explains why they’re feature-rich. Tkinter, Kivy, and wxPython are the free GUI libraries for Python.
What’s your favorite GUI library in Python?