A button or command button is probably the most common widget in any graphical user interface.
Click the button to command the computer to perform an action or answer a question. Typical buttons are OK, apply, cancel, close, yes, no and help.
A button is rectangular and usually displays a text label that describes its actions. Shortcuts can be specified for buttons.
Related course: Create PyQt Desktop Appications with Python (GUI)
QPushButton
The button widget is called QPushButton. The QPushButton widget provides a default button.
Start by importing QPushButton into your Python script:
from PyQt5.QtWidgets import QPushButton |
In the window constructor, add these lines which will add a button:
pybutton = QPushButton('Click me', self) |
The first line creates an object of the type QPushButton.
The first argument is the text shown on the button:
pybutton = QPushButton('Click me', self) |
The appearance depends on the operating system and the configured theme, something like this:
We resize it to 100 pixels in width and 32 in height.
pybutton.resize(100,32) |
Then we set it to position (50,50) on the window.
pybutton.move(50, 50) |
The click must be linked to a Python method, clickMethod().
pybutton.clicked.connect(self.clickMethod) |
pyqt5 button example
The program below creates a desktop window with a button inside of it.
By clicking the button it will call the method clickMethod().
The appearance of the window depends on the operating system.
import sys |
If you are new to Python PyQt, then I highly recommend this book.
PyQt Button example
The button can display a text label and can also select a small icon.
These can be set using constructor settings, which can be changed later using setText() and setIcon().
If the button is disabled, the appearance of the text and icons is related to the GUI style to make the button look “disabled.”
self.button3.setEnabled(False) |
When a button is activated by a mouse, space bar, or keyboard shortcut, the button sends a clicked() signal.
Connect to this signal to perform the action of the button. Buttons also provide less-used signals, such as pressed() and released().
self.button3.clicked.connect(self.Action) |
The program below shows various buttons in the window. It adds different types of buttons:
- Button 1 is a default button, a QPushButton
- Button 2 has a dropdown, by clicking it shows a menu QMenu
- Button 3 gets disabled for a few seconds and renabled using a QTimer

import sys |
In this example above, we implement three functions: menu buttons, buttons with countdowns (which are often encountered when accounts are registered) and a default click function.
The button menu QMenu is added to the button like this:
menu = QMenu(self) |
For the other example, we used the QTimer class, this is a time-related class.
The QTimer class provides repeatability and single timers. The QTimer class provides an advanced programming interface for timers.
To use it, create a QTimer, connect its timeout() signal to the appropriate slot, and then call start().
self.time = QTimer(self) |
From then on, it will send out signals at regular intervals. SetInterval() This property has a time-out interval in milliseconds. The default value for this property is 0.
If you are new to Python PyQt, then I highly recommend this book.
