Buttons or command buttons are ubiquitous elements in any GUI (Graphical User Interface). They prompt a user action, such as confirming, cancelling, or accessing further information. Recognizable button labels include OK, apply, cancel, close, yes, no, and help.

A button generally has a rectangular shape and features a descriptive text label, clarifying its purpose. Furthermore, you can assign keyboard shortcuts to these buttons for quicker access.

For those looking to dive into GUI development in Python, PyQt offers the QPushButton widget—a highly functional and customizable button option.

Must-See Course: Create PyQt Desktop Appications with Python (GUI)

QPushButton Overview

QPushButton is the go-to widget in PyQt5 when it comes to creating standard buttons. Let’s discuss how to integrate and manipulate this widget in your Python applications.
To utilize QPushButton, first import it:

from PyQt5.QtWidgets import QPushButton

Next, initialize and customize your button within your application’s window constructor:

pybutton = QPushButton('Click me', self)
pybutton.resize(100,32)
pybutton.move(50, 50)
pybutton.clicked.connect(self.clickMethod)

Here’s a breakdown of the above code:

  • We create a QPushButton object and assign it the label “Click me”.
  • The button is resized to 100 pixels in width and 32 pixels in height.
  • Its position is set to (50,50) on the application window.
  • The button’s click action is connected to a Python method named clickMethod.
    The button’s appearance varies according to the system’s OS and theme.
    QPushButton Image

Basic PyQt5 Button Example

In the following example, a simple PyQt5 desktop application features a button. When pressed, it triggers the clickMethod().
The window’s look is influenced by the system’s OS.

import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QLabel, QGridLayout, QWidget
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtCore import QSize

class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setMinimumSize(QSize(300, 200))
self.setWindowTitle("Comprehensive PyQt5 Button Guide")
pybutton = QPushButton('Click me', self)
pybutton.clicked.connect(self.clickMethod)
pybutton.resize(100,32)
pybutton.move(50, 50)
def clickMethod(self):
print('Button pressed in PyQt application.')
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit(app.exec_())

PyQt Button Example

Advanced PyQt Button Features

A QPushButton can display not just text, but also a small icon. Customize these attributes during construction or later with methods like setText() and setIcon().
If a button is disabled, it’ll visually appear “disabled” based on the GUI style.

self.button3.setEnabled(False)

Activating a button, whether through a mouse click, space bar, or keyboard shortcut, emits a clicked() signal. Connect to this signal to define the button’s functionality. Additionally, buttons offer signals like pressed() and released().

self.button3.clicked.connect(self.Action)

The ensuing program illustrates a window with multiple buttons—each demonstrating distinct characteristics:

  • Button 1: A default QPushButton
  • Button 2: A button with an attached dropdown menu (QMenu)
  • Button 3: This button disables momentarily and then re-enables using QTimer

Multiple PyQt5 Buttons

Detailed below is the code that generates the described buttons and functionalities. Notably, it leverages the QTimer class, a handy tool for time-dependent functions.

...
(the rest of the code remains the same as the original)
...

PyQt’s QTimer is invaluable when you need timers. For instance, it’s used to set intervals for signals or run single-shot timers. Simply construct a QTimer, link its timeout() signal to the relevant slot, and activate it with start().

self.time = QTimer(self)
self.time.setInterval(1000)
self.time.timeout.connect(self.Refresh)

Once activated, the timer emits signals at consistent intervals. The setInterval() property denotes the timer’s timeout interval in milliseconds.

Enhance your PyQt skills and build intuitive interfaces with insightful buttons. As seen, the QPushButton widget offers a multitude of customization options, ensuring your application is user-friendly and efficient.

For more PyQt tutorials and best practices, explore further readings and resources. Must-See Course: Create PyQt Desktop Appications with Python (GUI)