In this PyQt5 article iam going to show you How To Create QSpinBox In PyQt5. also iam going to show you how you can connect valueChanged Signal to PyQt5 Slot. The QSpinBox class provides a spin box widget.QSpinBox is designed to handle integers and discrete sets of values (e.g., month names); use QDoubleSpinBox for floating point values.
What is PyQt5 QSpinBox ?
PyQt5 QSpinBox is a widget that allows users to select a numerical value within a specified range by clicking arrow buttons or typing the value directly into the spin box. it is similar to regular QLineEdit widget, but it has up and down arrow buttons to increment or decrement the value, and it also enforces a range of values that the user can select from.
QSpinBox is part of the QtWidgets module in PyQt5 and inherits from QAbstractSpinBox. It can be customized by setting the range of values it can display, the step size for the increment and decrement buttons, the displayed number of digits, and other properties.
Also you can read more Python GUI articles in the below links
- Kivy GUI Development Tutorials
- TKinter GUI Development Tutorials
- Pyside2 GUI Development
- wxPython GUI Development Tutorials
- PyQt5 GUI Development Tutorials
First we need some imports.
1 2 3 4 |
from PyQt5 import QtGui from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QSpinBox, QLabel import sys from PyQt5.QtCore import Qt |
Now we need to create our main Window class that extends from QWidget, in the class first we set our window icon, window title and window geometry.
1 2 3 4 5 6 7 8 9 10 11 12 |
class Window(QWidget): def __init__(self): super().__init__() self.title = "PyQt5 QSpingBox" self.top = 200 self.left = 500 self.width = 200 self.height = 100 self.setWindowIcon(QtGui.QIcon("icon.png")) self.setWindowTitle(self.title) self.setGeometry(self.left, self.top, self.width, self.height) |
In here we have created the object of QVBoxLayout with QSpinBox.
1 2 |
vbox= QVBoxLayout() self.spinbox = QSpinBox(self) |
So now we have connected the valueChanged signal of QSpinBox with spin_changed() method.
1 |
self.spinbox.valueChanged.connect(self.spin_changed) |
And this is the method or slot that we have connected with valueChanged signal at the top.
1 2 3 |
def spin_changed(self): spinValue = self.spinbox.value() self.label.setText("Current Value Is : " + str(spinValue)) |
Also every PyQt5 application must create an application object.
1 |
App = QApplication(sys.argv) |
Finally, we enter the mainloop of the application. The event handling starts from this point.
1 2 |
window = Window() sys.exit(App.exec_()) |
Complete source code for How To Create QSpinBox In PyQt5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
from PyQt5 import QtGui from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QSpinBox, QLabel import sys from PyQt5.QtCore import Qt class Window(QWidget): def __init__(self): super().__init__() self.title = "PyQt5 QSpingBox" self.top = 200 self.left = 500 self.width = 200 self.height = 100 self.setWindowIcon(QtGui.QIcon("icon.png")) self.setWindowTitle(self.title) self.setGeometry(self.left, self.top, self.width, self.height) vbox= QVBoxLayout() self.spinbox = QSpinBox(self) self.spinbox.valueChanged.connect(self.spin_changed) vbox.addWidget(self.spinbox) self.label = QLabel() self.label.setAlignment(Qt.AlignCenter) self.label.setFont(QtGui.QFont("Sanserif", 15)) vbox.addWidget(self.label) self.setLayout(vbox) self.show() def spin_changed(self): spinValue = self.spinbox.value() self.label.setText("Current Value Is : " + str(spinValue)) App = QApplication(sys.argv) window = Window() sys.exit(App.exec_()) |
Run the complete code and this is the result

Also you can watch the complete video for this article
Like!! I blog quite often and I genuinely thank you for your information. The article has truly peaked my interest.