PyQt QPushButton

Summary: in this tutorial, you’ll learn how to use the PyQt QPushButton widget to create a push button.

Introduction to the PyQt QPushButton widget

The PyQt QPushButton class allows you to create a button widget, which can be a push button or a toggle button.

To create a push button, you follow these steps:

First, import QPushButton from PyQt6.QtWidgets module:

from PyQt6.QtWidgets import QPushButtonCode language: Parser3 (parser3)

Second, call the QPushButton() with a text that appears on the button:

button = QPushButton('Click Me')Code language: Python (python)

Third, connect the clicked signal to a callable:

button.clicked.connect(self.on_clicked)Code language: Python (python)

The on_clicked is a method that executes when the button is clicked.

The following shows the complete program that displays a button on a window:

import sys
from PyQt6.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout


class MainWindow(QWidget):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.setWindowTitle('PyQt QPushButton Widget')
        self.setGeometry(100, 100, 320, 210)

        button = QPushButton('Click Me')

        # place the widget on the window
        layout = QVBoxLayout()
        layout.addWidget(button)
        self.setLayout(layout)

        # show the window
        self.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)

    # create the main window
    window = MainWindow()

    # start the event loop
    sys.exit(app.exec())Code language: Python (python)

Output:

Creating a push button with an icon

To create a button with an icon, you use the following steps:

First, import QIcon from PyQt6.QtGui module:

from PyQt6.QtGui import QIconCode language: Python (python)

Second, create a QPushButton object:

button = QPushButton('Delete')Code language: Python (python)

Third, add the icon of the button by calling the setIcon() method of the QPushButton with the QIcon object:

button.setIcon(QIcon('trash.png'))Code language: Python (python)

Note that the QIcon object accepts a path to the icon file. To show the trash.png icon, you can download it here:

To make the button nicer, you can set its size by calling setFixedSize() method.

The size is determined by the QSize object with two arguments width and height. Note that you need to import QSize class from PyQt6.QtCore module.

The following program shows how to display a button with an icon:

import sys
from PyQt6.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout
from PyQt6.QtCore import QSize
from PyQt6.QtGui import QIcon


class MainWindow(QWidget):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.setWindowTitle('PyQt QPushButton Widget')
        self.setGeometry(100, 100, 320, 210)

        button = QPushButton('Delete')
        button.setIcon(QIcon('trash.png'))

        button.setFixedSize(QSize(100, 30))

        # place the widget on the window
        layout = QVBoxLayout()
        layout.addWidget(button)
        self.setLayout(layout)

        # show the window
        self.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)

    # create the main window
    window = MainWindow()

    # start the event loop
    sys.exit(app.exec())Code language: Python (python)

Output:

Creating a toggle button

The QPushButton class has the checkable property that allows you to use the button as a toggle button.

A toggle button has an on/off state. If the button is on, the checked button is true. Otherwise, it is false.

For a toggle button, the clicked signal sends the status of the button, either on or off.

The following program displays a window that has a toggle button:

import sys
from PyQt6.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout


class MainWindow(QWidget):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.setWindowTitle('PyQt QPushButton Widget')
        self.setGeometry(100, 100, 320, 210)

        button = QPushButton('Toggle Me')
        button.setCheckable(True)
        button.clicked.connect(self.on_toggle)

        # place the button on the window
        layout = QVBoxLayout()
        layout.addWidget(button)
        self.setLayout(layout)

        # show the window
        self.show()

    def on_toggle(self, checked):
        print(checked)


if __name__ == '__main__':
    app = QApplication(sys.argv)

    # create the main window
    window = MainWindow()

    # start the event loop
    sys.exit(app.exec())Code language: Python (python)

Summary

  • Use the PyQt QPushButton widget to create a push button or a toggle button.
Did you find this tutorial helpful ?