DEV Community

ShadowClaw11
ShadowClaw11

Posted on • Originally published at coderslegacy.com

PyQt5 tutorial | Python GUI with Qt

This article is quick overview on the GUI library PyQt5. By the end of this article, you'll know what PyQt5 is, it's benefits as compared to other GUI libraries, it's widgets and how to set it up.

What is PyQt5?

PyQt5 is a Python binding of the popular GUI framework Qt. Furthermore, PyQt5 is a cross-platform library, which means it works on all platforms such as Mac, Linux and Windows. As a more newer and updated GUI framework, PyQt5's GUI's are more modern and better looking than other GUI libraries like Tkinter.

Tkinter is actually the more popular GUI library due to it's age and community support. However, that doesn't mean that it's better than PyQt5, which edges it out in several areas (most notably it's look and cross platform ability). If you're interested in a more detailed comparison of PyQt5 and Tkinter, follow this link.

PyQt5 Installation

PyQt5 doesn't come with the Python Standard Library so you'll have to install it yourself. It's pretty simple if you're using pip, where you just have to run the following command in the command prompt. "pip install pyqt5". Remember to keep everything lowercase.

Once you've successfully installed it, try running the import pyqt5 command in a Python IDE. If it executes without a "Missing Module Error", then you're good to go.

PyQt5 Widgets

PyQt5 makes use of widgets, which are small GUI components like buttons and entry fields. Using the inbuilt functions that PyQt5 provides you with, you can easily create and modify these widgets as you wish.

To help you get the feel of it, we'll be posting the code for several PyQt5 widgets and their respective outputs down below.

QPushButton

A QPushButton is a simple widget used for creating buttons in PyQt5. When clicked it triggers a function that it's been connected to using the connect() function.

from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys

app = QApplication(sys.argv)
win = QMainWindow()
win.setGeometry(400,400,500,300)
win.setWindowTitle("CodersLegacy")

button = QtWidgets.QPushButton(win)
button.clicked.connect(win.close)
button.setText("Quit Button")
button.resize(120,60)
button.move(350,220)

win.show()
sys.exit(app.exec_())

PyQt5 button

QLineEdit

The simplest way of taking input from the User in PyQt5. However, this method is only used to take single line input, for multiple line input you will need to use the QTextEdit widget.

from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys

def show():
    print(line.text())

app = QApplication(sys.argv)
win = QMainWindow()
win.setGeometry(400,400,300,300)
win.setWindowTitle("CodersLegacy")

line = QtWidgets.QLineEdit(win)
line.move(100,80)

win.show()
sys.exit(app.exec_())

PyQt5 QLineEdit

QComboBox

Used to present the User with multiple options from which he may select one in a visually pleasing manner (through the use of a drop down menu).

from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys

app = QApplication(sys.argv)
win = QMainWindow()
win.setGeometry(400,400,300,300)
win.setWindowTitle("CodersLegacy")

combo = QtWidgets.QComboBox(win)
combo.addItem("Python")
combo.addItem("Java")
combo.addItem("C++")
combo.move(100,100)

win.show()
sys.exit(app.exec_())

PyQt5 QComboBox

If you want to know more about PyQt5 and it's widgets, you can learn about them from our tutorials series at CodersLegacy.

Top comments (0)