In this article we are going to learn How to Create SpinBox with Python & PyQt, QSpinBox is designed to handle integers and discrete sets of values QSpinBox allows the user to choose a value by clicking the up/down buttons or pressing up/down on the keyboard to increase/decrease the value currently displayed. The user can also type the value in manually. there are different method that you can use in QSpinBox for example we have value() and this method returns the current selected integer value in spinbox , we have setMinium() and setMaximum() methods, also there are some signals for QSpinBox, for example we have valueChanged() signal and it is emitted when the value of spinbox is changed also we have editingFinished() signal and it is emitted when the focus is lost on the spinbox.
- Build Text to Speech App with Python & PyQt5
- How to Build GUI Window in PyQt6
- How to Show Image in PyQt6
- How to Create Calendar with Python & PyQt6
- How to Create CheckBox in PyQt6
This is the complete code for How to Create SpinBox with Python & PyQt
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
from PyQt6.QtWidgets import QApplication, QWidget,\ QHBoxLayout, QSpinBox, QLineEdit, QLabel from PyQt6.QtGui import QIcon import sys class Window(QWidget): def __init__(self): super().__init__() self.setGeometry(200, 200, 700, 400) self.setWindowTitle("PyQt6 QSpinBox") self.setWindowIcon(QIcon('python.png')) # cretae hboxlayout hbox = QHBoxLayout() # in here we have created our label label = QLabel("Laptop Price : ") # this is our linedit self.lineEdit = QLineEdit() # we need to create the object of QSpinBox class self.spinbox = QSpinBox() # we have connected valueChanged signal self.spinbox.valueChanged.connect(self.spin_selected) self.totalResult = QLineEdit() # add widgets to your hbox layout hbox.addWidget(label) hbox.addWidget(self.lineEdit) hbox.addWidget(self.spinbox) hbox.addWidget(self.totalResult) self.setLayout(hbox) def spin_selected(self): if self.lineEdit.text() != 0: price = int(self.lineEdit.text()) totalPrice = self.spinbox.value() * price self.totalResult.setText(str(totalPrice)) else: print("Wrong value") app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec()) |
In here we have created the width and height also x and y position for the window.
1 |
self.setGeometry(200, 200, 700, 400) |
This is used for adding title to the window.
1 |
self.setWindowTitle("PyQt6 QSpinBox") |
In here we have created an icon, make sure that you have already added an icon to your working directory.
1 |
self.setWindowIcon(QIcon('python.png')) |
This is our label, for creating of the label you can use QLabel class.
1 |
label = QLabel("Laptop Price : ") |
In here we have created our QLineEdit.
1 |
self.lineEdit = QLineEdit() |
Now we are going to create our spinbox in python & pyqt, for creating of spinbox we can use QSpinBox class.
1 |
self.spinbox = QSpinBox() |
As we have already said that there are different signals in QspinBox
that you can use, in here we are using toggled() signal of QSpinBox, we have
connected that with spin_selected method or slot.
1 |
self.spinbox.valueChanged.connect(self.spin_selected) |
This is the spin_selected() method, in this method we are getting the value
from QLineEdit and after that we multiply that with the spinbox value and
at the end we add the result in the totalResult label.
1 2 3 4 5 6 7 8 9 10 |
def spin_selected(self): if self.lineEdit.text() != 0: price = int(self.lineEdit.text()) totalPrice = self.spinbox.value() * price self.totalResult.setText(str(totalPrice)) else: print("Wrong value") |
Run the complete code and this will be the result.
