
- PyQt - Home
- PyQt - Introduction
- PyQt - Environment
- PyQt - Hello World
- PyQt - Major Classes
- PyQt - Using Qt Designer
- PyQt - Meta Objects
- PyQt Signals & Slots
- PyQt - Signals and Slots
- PyQt - Support and Signals
- PyQt - Unbound and Bound Signals
- PyQt - New Signals with PyQtSignal
- PyQt - Connecting, Disconnecting, & Emitting Signals
- PyQt - Slot decorator
- PyQt - Slot Connection
- PyQt Layouts
- PyQt - Layout Management
- PyQt - QBoxLayout
- PyQt - QGridLayout
- PyQt - QFormLayout
- PyQt - QHBoxLayout
- PyQt - QVBoxLayout
- PyQt - QStackedLayout
- PyQt - QGraphicsGridLayout
- PyQt - QGraphicsAnchorLayout
- PyQt - QGraphicsLayout
- PyQt - QGraphicsLinearLayout
- PyQt Basic Widgets
- PyQt - Basic Widgets
- PyQt - Qlabel Widget
- PyQt - QLineEdit Widget
- PyQt - QPushButton Widget
- PyQt - QRadioButton Widget
- PyQt - QCheckBox Widget
- PyQt - QComboBox Widget
- PyQt - QSpinBox Widget
- PyQt - QMessageBox
- PyQt - QDialogButtonBox Widget
- PyQt - QFontComboBox Widget
- PyQt - QDoubleSpinBox Widget
- PyQt - QToolBox Widget
- PyQt - QMenuBar, QMenu & Qaction Widgets
- PyQt - QToolTip
- PyQt - QInputDialog Widget
- PyQt - QFontDialog Widget
- PyQt - QDialog Widget
- PyQt - QFileDialog Widget
- PyQt - QTab Widget
- PyQt - QSplitter Widget
- PyQt - QDock Widget
- PyQt - QStatusBar Widget
- PyQt - QTabBar
- PyQt - QList Widget
- PyQt - QScrollBar Widget
- PyQt - QProgressBar
- PyQt - QCalendar Widget
- PyQt - QMessageBox Widget
- PyQt - QPlainTextEdit
- PyQt - QDateEdit
- PyQt - QDateTimeEdit
- PyQt - QTimeEdit
- PyQt - QTextEdit
- PyQt - QTextBrowser
- PyQt - QScrollArea
- PyQt - Drag and Drop
- PyQt - Multiple Document Interface
- PyQt - QDialog Class
- PyQt Views
- PyQt - QColumnView
- PyQt - QTableView
- PyQt Drawing API
- PyQt - Drawing API
- PyQt - Drawing a Line
- PyQt - Drawing a Rectangle
- PyQt - Drawing a Triangle
- PyQt - Drawing a Circle
- PyQt - Drawing a Ellipse
- PyQt - Drawing a Polygon
- PyQt - Geometric Transformation
- PyQt - Drawing Effect
- PyQt Groups
- PyQt - QButtonGroup
- PyQt - QGroupBox
- PyQt Effects
- PyQt - Effects
- PyQt - Opacity Effect
- PyQt - QGraphicsBlur Effect
- PyQt - QGraphicsColorize Effect
- PyQt - QGraphicsDropShadow Effect
- PyQt Events
- PyQt - Event Handling
- PyQt - Drag & Drop Events
- PyQt - File Open Event
- PyQt - Action Event
- PyQt - Hide Event
- PyQt - Resize Event
- PyQt Database
- PyQt - Database Handling
- PyQt Essentials
- PyQt - BrushStyle Constants
- PyQt - QClipboard
- PyQt - QPixmap Class
- PyQt Useful Resources
- PyQt - Quick Guide
- PyQt - Useful Resources
- PyQt - Discussion
PyQt - QPushButton Widget
In any GUI design, the command button is the most important and most often used control. Buttons with Save, Open, OK, Yes, No and Cancel etc. as caption are familiar to any computer user. In PyQt API, the QPushButton class object presents a button which when clicked can be programmed to invoke a certain function.
Inheritance Diagram
QPushButton class inherits its core functionality from QAbstractButton class which extracts its properties from QWidgets class. A command button is rectangular and usually displays a text label describing its action. It can also have a shortcut key specified using the ampersand symbol in the text, enabling keyboard activation.

Example
button = QPushButton("&Download", self)
In the above example if we press Alt+D then the button will be activated.
Working of QPushButton
QPushButton can display both textual labels and icons. We can set these properties of the button during initialization or modify them later using methods like setText() and setIcon(). When the button is disabled then the appearence of the text and icon adjusts to indicate that the button is in disabled state.
QPushButton emits the clicked() signal when activated by the mouse, Spacebar, or a keyboard shortcut. This signal is connected to perform the button's associated action. Additionally, it provides signals like pressed() and released() for less common use cases.
In dialogs, command button are set to auto default buttons i.e they become the default push button automatically when they receive keyboard focus.The default button is activated when the user presses Enter or Return key in the dialog. This behavior can be customized using setAutoDefault().
Usage of QPushButton
Push buttons can be used for actions that are triggered by user interaction, such as Apply, Cancel, or Help. The button have a wide, rectangular shape with a text label. For small, square buttons that change the window's state, tool buttons (QToolButton) are more appropriate.
Variations and Customizations
QPushButton offers various customization options, including toggle behavior (setCheckable()), auto-repeat functionality (setAutoRepeat()), and controlling button appearance. It supports different states like available or not, standard or menu button, on or off (for toggling push buttons), default or normal, auto-repeat or not, and pressed down or not.
Methods of QPushButton Class
Following are some of the most commonly used methods of QPushButton class −
Sr.No. | Methods & Description |
---|---|
1 |
setCheckable() Recognizes pressed and released states of button if set to true |
2 |
toggle() Toggles between checkable states |
3 |
setIcon() Shows an icon formed out of pixmap of an image file |
4 |
setEnabled() When set to false, the button becomes disabled, hence clicking it doesnt emit a signal |
5 |
isChecked() Returns Boolean state of button |
6 |
setDefault() Sets the button as default |
7 |
setText() Programmatically sets buttons caption |
8 |
text() Retrieves buttons caption |
Example
Four QPushButton objects are set with some of the above attributes. The example is written in object oriented form, because the source of the event is needed to be passed as an argument to slot function.
Four QPushButton objects are defined as instance variables in the class. First button b1 is converted into toggle button by the statements −
self.b1.setCheckable(True) self.b1.toggle()
Clicked signal of this button is connected to a member method btnstate() which identifies whether button is pressed or released by checking isChecked() property.
def btnstate(self): if self.b1.isChecked(): print "button pressed" else: print "button released"
Second button b2 displays an icon on the face. setIcon() method takes a pixmap object of any image file as argument.
b2.setIcon(QIcon(QPixmap("python.gif")))
Button b3 is set to be disabled by using setEnabled() method −
b3.setEnabled(False)
PushButton b4 is set to default button by setDefault() method. Shortcut to its caption is created by prefixing & to the caption (&Default). As a result, by using the keyboard combination Alt+D, connected slot method will be called.
Buttons b1 and b4 are connected to whichbtn() slot method. Since the function is intended to retrieve caption of the clicked button, the button object should be passed as an argument. This is achieved by the use of lambda function.
For example,
b4.clicked.connect(lambda:self.whichbtn(self.b4))
The complete code is given below −
import sys from PyQt6.QtCore import * from PyQt6.QtGui import * from PyQt6.QtWidgets import QDialog,QApplication, QPushButton,QVBoxLayout class Form(QDialog): def __init__(self, parent=None): super(Form, self).__init__(parent) layout = QVBoxLayout() self.b1 = QPushButton("Button1") self.b1.setCheckable(True) self.b1.toggle() self.b1.clicked.connect(lambda:self.whichbtn(self.b1)) self.b1.clicked.connect(self.btnstate) layout.addWidget(self.b1) self.b2 = QPushButton() self.b2.setIcon(QIcon(QPixmap("python.gif"))) self.b2.clicked.connect(lambda:self.whichbtn(self.b2)) layout.addWidget(self.b2) self.setLayout(layout) self.b3 = QPushButton("Disabled") self.b3.setEnabled(False) layout.addWidget(self.b3) self.b4 = QPushButton("&Default") self.b4.setDefault(True) self.b4.clicked.connect(lambda:self.whichbtn(self.b4)) layout.addWidget(self.b4) self.setWindowTitle("Button demo") def btnstate(self): if self.b1.isChecked(): print ("button pressed") else: print ("button released") def whichbtn(self,b): print ("clicked button is "+b.text()) def main(): app = QApplication(sys.argv) ex = Form() ex.show() sys.exit(app.exec()) if __name__ == '__main__': main()
Output
The above code produces the following output.

clicked button is Button1 button released clicked button is Button1 button pressed clicked button is &Default
Example: QPushButton with Menu
Let's create a QPushButton with menu options.
import sys from PyQt6.QtWidgets import QApplication, QWidget, QPushButton, QMenu class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): btn = QPushButton('Menu', self) menu = QMenu(self) menu.addAction('Option 1') menu.addAction('Option 2') btn.setMenu(menu) self.setGeometry(300, 300, 300, 200) self.setWindowTitle('Menu QPushButton') self.show() def main(): app = QApplication(sys.argv) ex = Example() sys.exit(app.exec()) if __name__ == '__main__': main()
Output
The above code produces the following result −
