
- 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 - QSpinBox Widget
A QSpinBox object presents the user with a textbox which displays an integer with up/down button on its right. The value in the textbox increases/decreases if the up/down button is pressed.
By default, the integer number in the box starts with 0, goes upto 99 and changes by step 1. Use QDoubleSpinBox for float values.
Methods Used in QSpinBox Widget
Important methods of QSpinBox class are listed in the following table −
Sr.No. | Methods & Description |
---|---|
1 |
setMinimum() Sets the lower bound of counter |
2 |
setMaximum() Sets the upper bound of counter |
3 |
setRange() Sets the minimum, maximum and step value |
4 |
setValue() Sets the value of spin box programmatically |
5 |
Value() Returns the current value |
6 |
singleStep() Sets the step value of counter |
QSpinBox object emits valueChanged() signal every time when up/own button is pressed. The associated slot function can retrieve current value of the widget by value() method.
Basic Structure of QSpinBox
Before getting into the examples, let's understand the basic structure of a QSpinBox widget.
In this code snippet we're creating a PyQt window featuring a QSpinBox widget. Next we define the upper boundaries, for the spin box by using the setMinimum() and setMaximum() functions. Additionally we initialize the spin box starting value with setValue().
from PyQt6.QtWidgets import QApplication, QMainWindow, QSpinBox class MyWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("Basic QSpinBox Example") self.setGeometry(200, 200, 300, 200) spinbox = QSpinBox(self) spinbox.move(100, 50) spinbox.setMinimum(0) spinbox.setMaximum(100) spinbox.setValue(50) if __name__ == "__main__": app = QApplication([]) window = MyWindow() window.show() app.exec()
Output
The above code produces the following output −

Example 1: Customized QSpinBox
In the below example, we show the customization options for the QSpinBox widget. We set the minimum and maximum values to -100 and 100, respectively. Additionally, we adjust the step size for incrementing or decrementing values using setSingleStep().
from PyQt6.QtWidgets import QApplication, QMainWindow, QSpinBox class MyWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("Customized QSpinBox Example") self.setGeometry(200, 200, 300, 200) spinbox = QSpinBox(self) spinbox.move(100, 50) spinbox.setMinimum(-100) spinbox.setMaximum(100) spinbox.setSingleStep(5) spinbox.setValue(0) if __name__ == "__main__": app = QApplication([]) window = MyWindow() window.show() app.exec()
Output
The above code produces the following output −

Example 2: QSpinBox Signal Handling
In this example, we connect the valueChanged signal of the QSpinBox widget to a custom slot on_value_changed. This slot updates a QLabel with the currently selected value whenever the value in the spin box changes.
from PyQt6.QtWidgets import QApplication, QMainWindow, QSpinBox, QLabel class MyWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("QSpinBox Signal Handling Example") self.setGeometry(200, 200, 300, 200) spinbox = QSpinBox(self) spinbox.move(100, 50) spinbox.setMinimum(0) spinbox.setMaximum(100) spinbox.valueChanged.connect(self.on_value_changed) self.label = QLabel(self) self.label.move(100, 100) self.label.setText("Selected Value: 0") def on_value_changed(self, value): self.label.setText(f"Selected Value: {value}") if __name__ == "__main__": app = QApplication([]) window = MyWindow() window.show() app.exec()
Output
The above code produces the following output −
