Home ยป The PyQt QPushButton widget

The PyQt QPushButton widget

Java SE 11 Programmer I [1Z0-815] Practice Tests
Oracle Java Certification
Java SE 11 Programmer II [1Z0-816] Practice Tests
Spring Framework Basics Video Course
1 Year Subscription
Java SE 11 Developer (Upgrade) [1Z0-817]

In this article we will look at the PyQt QPushButton widget.

The QPushButton is a button in PyQt, when clicked on by a user an associated action gets performed.

First we will look at some of the more frequently used methods and signals relating to the QPushButton

Frequently used methods

Method Description
setCheckable() If you set it as True, it distinguishes between the pressed and unpressed state.
toggle() Changes the state.
setIcon() Sets the icon of the button.
setEnabled() If you set it as False, you cannot use the button.
isChecked() Returns whether a button is selected or not.
setText() Sets the text to be displayed on the button.
text() Returns the text displayed on the button.

Frequently used signals

Signal Description
clicked() Generated when a button is clicked.
pressed() Generated when a button is pressed.
released() Generated when a button is released.
toggled() Generated when the state of the button changes.

 

Examples

Here is a simple example for starters that displays a basic button

 

#!/usr/bin/python

import sys
from PyQt5.QtWidgets import QWidget, QPushButton, QApplication, QHBoxLayout

class Example(QWidget):

    def __init__(self):
        super().__init__()
        self.initUI()


    def initUI(self):
        hbox = QHBoxLayout()

        quitbtn = QPushButton('Quit', self)
        quitbtn.clicked.connect(QApplication.instance().quit)

        hbox.addWidget(quitbtn)
        hbox.addStretch(1)

        self.setLayout(hbox)
        self.move(300, 300)
        self.setWindowTitle('Qbutton Example')
        self.show()


def main():
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

Lets look at the QPushButton related code

The QPushButton is placed in a QBoxLayout which is called hbox. We then create a button with the text Quit in it

quitbtn = QPushButton('Quit', self)

We then connect the clicked signal of the QPushButton to the QApplication's quit function.

quitbtn.clicked.connect(QApplication.instance().quit)

We then add the button to the QBoxLayout

QpushButton with icon

Now lets add an icon to the button

We can do this by passing a QIcon as the first parameter to QPushButton. The image was in the same directory location as the script

#!/usr/bin/python
import sys
from PyQt5.QtWidgets import QWidget, QPushButton, QApplication, QHBoxLayout
from PyQt5.QtGui import QIcon

class Example(QWidget):

    def __init__(self):
        super().__init__()
        self.initUI()


    def initUI(self):
        hbox = QHBoxLayout()

        quitbtn = QPushButton(QIcon('exit.png'),'Exit', self)
        quitbtn.clicked.connect(QApplication.instance().quit)

        hbox.addWidget(quitbtn)
        hbox.addStretch(1)

        self.setLayout(hbox)
        self.move(300, 300)
        self.setWindowTitle('Qbutton Example')
        self.show()


def main():
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

And when run this displayed the following

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More