PyQt6–Make your frontend beautiful with Python

Binay Pradhan
3 min readMar 5, 2024

--

What PyQt6 approch to use, to have more control on rendering the GUI?

The provided example is an approach to elaborate the use of PyQt6 application designed for an interaction with user responses. As PyQt6 is an event driven apprication, meaning “when an event is send/received the application gui responds.” we need to be careful on one core aspect “change of variable data needs a refresh of GUI”

A Basic Example Usage

We shall keep expanding on this example.

Libraries needed to be installed are all listed here:

#tested on python 3.10+
pip install PyQt6
  • QtCore: Provides core non-GUI functionality, like signal and slots, properties, base classes of item models, serialization, and more.
  • QtWidgets: Provides ready to use Widgets for your application, including graphical elements for your UI.
  • QtGui: Extends QtCore with GUI functionality: Events, windows and screens, OpenGL and raster-based 2D painting, as well as images

These are the top level modules of PyQt6 that contains all the components needed to build an application.

import sys
import PyQt6.QtCore as qtc
import PyQt6.QtWidgets as qtw
import PyQt6.QtGui as qtg

class PrintApp(qtw.QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('PrintApp')
layout = qtw.QVBoxLayout(self) # Important

self.line_edit = qtw.QLineEdit(self)
self.line_edit.setPlaceholderText('Provide a text...')

self.print_button = qtw.QPushButton('Display on PrintApp', self)
self.print_button.clicked.connect(self.display)

layout.addWidget(self.line_edit)
layout.addWidget(self.print_button)

self.setLayout(layout) # Important

def display(self):
print(self.line_edit.text())

if __name__ == '__main__':
app = qtw.QApplication(sys.argv)
ex = PrintApp()
ex.show() # Important
sys.exit(app.exec())

The execution of the above script, provides us an interface as such:

An application with PyQt6 elements
PrintApp application interface

If you have guessed that QVBoxLayout is the one that arranges and contains all elements objects, then you were right. Note that the layoutvariable needs to be set using self.setLayout(layout) in this example.

This approach assumes all the widgets are displayed at once when the layout is set.

This approach is very useful when you wish to replace one element with another when an event is triggered. (example: On click of “Display on PrintApp” button)

Modified code

import sys
import PyQt6.QtCore as qtc
import PyQt6.QtWidgets as qtw
import PyQt6.QtGui as qtg

class PrintApp(qtw.QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('PrintApp')
layout = qtw.QVBoxLayout(self) # Important

self.line_edit = qtw.QLineEdit(self)
self.line_edit.setPlaceholderText('Provide a text...')

self.line_text = qtw.QLabel() # QLabel Added
self.line_text.hide() # Intially hidden, as we intend to show this when the button is clicked

self.print_button = qtw.QPushButton('Display on PrintApp', self)
self.print_button.clicked.connect(self.display)

layout.addWidget(self.line_edit)
layout.addWidget(self.line_text)
layout.addWidget(self.print_button)

self.setLayout(layout) # Important

def display(self):
self.line_edit.hide() # Hide
self.line_text.setText(f"You entered: {self.line_edit.text()}")
self.line_text.show() # Show

if __name__ == '__main__':
app = qtw.QApplication(sys.argv)
ex = PrintApp()
ex.show() # Important
sys.exit(app.exec())

Upon executing and providing a text, you should see the following transition:

PrintApp appliction with same layout, but using hide() and show() to replace element

--

--

Binay Pradhan
Binay Pradhan

Written by Binay Pradhan

👨🏻‍💻 Masters Student AI, BTU, Germany. All views are mine. (binaypradhan.me)

No responses yet