python logo

pyqt5 button


Python hosting: Host, run, and code Python in the cloud!

PyQt5 offers the ability to incorporate buttons using its QPushButton class. This class is housed within the PyQt5.QtWidgets module. You can create a button by invoking the QPushButton constructor and passing the desired display text as an argument.

Related course:

Introduction
For integrating buttons into a PyQt5 application, it’s essential to modify the import line as follows:

1
2
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
from PyQt5.QtCore import pyqtSlot

Within the initUI() function, insert the subsequent code:

1
2
3
button = QPushButton('PyQt5 button', self)
button.setToolTip('This is an example button')
button.move(100,70)

The QPushButton initializes the widget with the button text specified in the first parameter. The setToolTip method displays a tooltip when a user hovers over the button. The button’s position is then adjusted to coordinates x=100, y=70.
To register a button click, we need to implement the following method:

1
2
3
@pyqtSlot()
def on_click(self):
print('PyQt5 button click')

Then, connect the aforementioned method to the button click event like so:

1
button.clicked.connect(self.on_click)

The finalized PyQt5 button code is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot

class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'PyQt5 button - pythonspot.com'
self.left = 10
self.top = 10
self.width = 320
self.height = 200
self.initUI()

def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
button = QPushButton('PyQt5 button', self)
button.setToolTip('This is an example button')
button.move(100,70)
button.clicked.connect(self.on_click)
self.show()

@pyqtSlot()
def on_click(self):
print('PyQt5 button click')

if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())

PyQt5 button example

A visual representation of the PyQt5 button from the aforementioned code is displayed above.

Download PyQT5 Examples with this link.

< Back | Next >






Leave a Reply:




Mrunal tawake Tue, 12 Jan 2021

How to connect more than one function to single push button?

Frank Tue, 12 Jan 2021

Buttons have one call back function. If you want to call multiple functions, you can do it inside the callback function. That means call another function inside the on_click function. You may be able to add another clicked.connect connect call, but I recmomend calling any functions you want to call in the on_click() function.

juerg maier 2021-05-02T12:28:40.449Z

Hi
Thanks for this example.
I have tried to create a test case to verify that the button wiring is working. With the help of a stackoverflow member this worked for me. May be worth to add a link?
https://stackoverflow.com/questions/67345474/extend-generated-testapp-to-verify-pyqt5-button-click
Regards, Jurg