PyQt5 Tutorials
About Lesson

In this PyQt5 lesson we want to learn about PyQt5 QSpinBox, so QSpinBox is designed to handle integers and discrete sets of values (e.g., month names), use QDoubleSpinBox for floating point 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.



These are the imports that we need, you can use QSpinBox class for creating of the spinbox in pyqt.



You can use this code for setting the x,y position, width and height of the window.



This is the title for our window.



If you want to set an icon for the window, than you can use this code, make sure that you have already added an icon in your working directory.



In this application we need two QLineEdit, one QLabel and one QSpinBox, also an hbox layout  for aligning horizontally our widgets.



We have connected the valueChanged() signal of spinbox with spin_selected() method that we are going to create.



You need to add all your widgets in the hbox layout.



At the end set the hbox layout for your main window, if you don’t do this you will not see any widgets in the window.



And this is the method that we have already connected with the valueChanged() signal of QSpinBox.



This is the code for PyQt5 QSpinBox




Run the complete code and this will be the result 

PyQt5 SpinBox - Working with QSpinBox in PyQt
PyQt5 SpinBox – Working with QSpinBox in PyQt



Creating QDoubleSpinBox with Qt Designer

OK now we want to create QDoubleSpinBox widget using Qt Designer, open your Qt Designer, you can just write pyqt5designer in your terminal, after opening the Qt Designer you need to create Dialog without Buttons window. now we add widgets in Qt Designer.

  • First add an HBoxLayout
  • Now you need to add four widgets in the HBoxLayout, QLabel,QLineEdit,QDoubleSpinBox and QLineEdit
PyQt5 QDoubleSpinBox
PyQt5 QDoubleSpinBox



After completing of the design, save the ui file, iam going to name it doublespin.ui. now copy the file and paste the file in your working directory, this time we are not going to convert the ui file in to python, but we directly load the ui file using uic module.


Create a new python file iam going to call it and add this code.


Also we need to find the child in our ui file using findChild() method, because we want to connect valueChanged() signal of the spinbox with spin_selected() method.


This is the method, basically in this method we get the value from the QLineEdit with text() method, also the spinbox value with the value() method.



Run the code and this is the result.

PyQt5 QDoubleSpinBox
PyQt5 QDoubleSpinBox