PyQt QPushButton

Push buttons or command buttons are buttons used to force a user to perform an action by issuing a command to a program, and is the most commonly used and important widget in GUI programming.

You can create a QPushButton class, which is often used with push buttons and signals/slots. Now let's create a push button using the QPushButton class.

Related course: Create Desktop Apps with Python PyQt5

QPushButton.

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

class MyApp(QWidget):

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

    def initUI(self):
        btn1 = QPushButton('&Button1', self)
        btn1.setCheckable(True)
        btn1.toggle()

        btn2 = QPushButton(self)
        btn2.setText('Button&2')

        btn3 = QPushButton('Button3', self)
        btn3.setEnabled(False)

        vbox = QVBoxLayout()
        vbox.addWidget(btn1)
        vbox.addWidget(btn2)
        vbox.addWidget(btn3)

        self.setLayout(vbox)
        self.setWindowTitle('QPushButton')
        self.setGeometry(300, 300, 300, 200)
        self.show()

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

I created three different push buttons.

pyqt qpushbutton button

Related course: Create Desktop Apps with Python PyQt5

Description

btn1 = QPushButton('&Button1', self)
btn1.setCheckable(True)
btn1.toggle()

Create one push button with the QPushButton class. The first parameter is the text that will appear on the button, and the second is the parent class to which the button belongs.

If you want to assign a shortcut to a button, you can put the ampersand ('&') in front of the character as shown below. The shortcut for this button will be 'Alt+b'.

If you set setCheckable() to True, you will be able to remain selected or undying.

Calling the toggle() method changes the state of the button. Therefore, this button is selected when the program starts.

btn2 = QPushButton(self)
btn2.setText('Button&2')

The setText() method also allows you to specify the text that will appear on the button.

In addition, the shortcut for this button will be 'Alt+2'.

btn3 = QPushButton('Button3', self)
btn3.setEnabled(False)

If setEnabled() is set to False, the button becomes unavailable.