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)
pybutton.resize(100,32)
pybutton.move(50, 50)
pybutton.clicked.connect(self.clickMethod)

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:

QPushButton pyqt button

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
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):
QMainWindow.__init__(self)

self.setMinimumSize(QSize(300, 200))
self.setWindowTitle("PyQt button example - pythonprogramminglanguage.com")

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

def clickMethod(self):
print('Clicked Pyqt button.')

if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit( app.exec_() )
pyqt button example, QPushButton

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

python pyqt5 buttons

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

class Example(QMainWindow):

def __init__(self):
QMainWindow.__init__(self)

self.setMinimumSize(QSize(320, 200))
self.setWindowTitle("PyQt button example - pythonprogramminglanguage.com")

self.bt1 = QPushButton("Button 1",self)
self.bt2 = QPushButton("Button 2",self)
self.bt3 = QPushButton('Button 3',self)

self.bt1.move(50,50)
self.bt2.move(50,100)
self.bt3.move(170,100)

menu = QMenu(self)
menu.addAction('Fruit')
menu.addSeparator()
menu.addAction('Cookies')
menu.addSeparator()
menu.addAction('Ice cream')
self.bt2.setMenu(menu)

self.bt1.clicked.connect(self.Button1)
self.count = 10
self.bt3.clicked.connect(self.Action)
self.time = QTimer(self)
self.time.setInterval(1000)
self.time.timeout.connect(self.Refresh)
self.show()

def Button1(self):
print('Clicked')

def Action(self):
if self.bt3.isEnabled():
self.time.start()
self.bt3.setEnabled(False)

def Refresh(self):
if self.count > 0:
self.bt3.setText(str(self.count)+' seconds')
self.count -= 1
else:
self.time.stop()
self.bt3.setEnabled(True)
self.bt3.setText('Button 3')
self.count = 10

if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainWin = Example()
mainWin.show()
sys.exit( app.exec_() )

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)                                                                                           
menu.addAction('Fruit')
menu.addSeparator()
menu.addAction('Cookies')
menu.addSeparator()
menu.addAction('Ice cream')
self.bt2.setMenu(menu)

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)
self.time.setInterval(1000)
self.time.timeout.connect(self.Refresh)

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.

Download PyQt Examples