Summary: in this tutorial, you’ll learn how to use the PyQt QSpinBox
widget to create a spin box.
Introduction to the PyQt QSpinBox widget
A spin box combines a text entry and an up-down control. The up-down control allows you to spin through a set of incremental values:
The values of a spin box can be integers or discrete sets of values e.g., days of weeks and months of years.
A spin box allows you to increase or decrease a value by clicking the up/down buttons or pressing the up/down key on the keyboard. Also, you can type a value manually in the spin box.
To create a spin box, you use the QSpintBox
class:
QSpinBox()
Code language: Python (python)
Whenever the value of a spin box changes, it emits the valueChanged()
signal that sends the current value as an integer.
In addition, the QSpinBox
emits the textChanged
signal that provides a spin box’s value as an instance of the QString
.
The following table lists some useful properties of the QSpinBox
:
Property | Description |
---|---|
value | The current integer value of the spin box. |
cleanText | The current string value of the spin box (excludes the prefix and suffix). |
maximum | The maximum integer value of the spin box |
minimum | The minimum integer value of the spin box. |
prefix | A string that prepends to the displayed value. |
suffix | A string that appends to the displayed value. |
singleStep | An increment/decrement integer value when up/down arrows are clicked |
wrapping | is a boolean value that determines whether to wrap from one end of the range to the other when the up/down arrows are clicked. |
Let’s take an example of using the PyQt QSpinBox
class.
PyQt QSpinBox example
The following program displays a spin box that allows you to enter an amount between one and 100:
import sys
from PyQt6.QtWidgets import QApplication, QWidget, QSpinBox, QLabel, QFormLayout
from PyQt6.QtCore import Qt
class MainWindow(QWidget):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setWindowTitle('PyQt QSpinBox')
self.setMinimumWidth(300)
# create a grid layout
layout = QFormLayout()
self.setLayout(layout)
amount = QSpinBox(minimum=1, maximum=100, value=20, prefix='$')
amount.valueChanged.connect(self.update)
self.result_label = QLabel('', self)
layout.addRow('Amount:', amount)
layout.addRow(self.result_label)
# show the window
self.show()
def update(self, value):
self.result_label.setText(f'Current Value: {value}')
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec())
Code language: Python (python)
How it works.
First, create a QSpinBox
object with the minimum, maximum, value, and prefix options:
amount = QSpinBox(minimum=1, maximum=100, value=20, prefix='$')
Code language: Python (python)
Second, connect the valueChanged
signal to the update()
method:
amount.valueChanged.connect(self.update)
Code language: Python (python)
Third, create a QLabel
for displaying the current value of the spin box:
self.result_label = QLabel('', self)
Code language: Python (python)
Finally, define the update()
method that changes the value of the result label whenever the value of the spin box changes:
def update(self, value):
self.result_label.setText(f'Current Value: {value}')
Code language: Python (python)
Summary
- Use
QSpinBox
class to create a spin box. - Connect to the
valueChanged
signal to trigger an action when the current value of a spin box changes