Skip to content
Related Articles

Related Articles

PyQt5 QSpinBox – Adding looping
  • Last Updated : 17 May, 2020

In this article we will see how we can add looping to the spin box, when we create a spin box by default when it reaches the maximum value it can’t be incremented any more similarly when it reaches minimum value it can’t be decremented any more. By adding looping to the spin box the values repeat it self after the maximum or minimum value is reached.

Implementation steps :

1. Create a window
2. Create a spin box
3. Set range of the spin box such that it has one extra value for minimum and maximum value for example if we want values from 0 to 100 set range to -1 to 101
4. Add action to the spin box such that every time its value changes action should get called
5. Inside the action get the current value of the spin box.
6. Check if current value is equal to the minimum value then set current value of spin box to maximum value – 1
7. Else check if current value is equal to maximum value then make current value of spin box to minimum + 1

Below is the implementation

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing libraries
from PyQt5.QtWidgets import * 
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import * 
from PyQt5.QtCore import * 
import sys
  
  
class Window(QMainWindow):
  
    def __init__(self):
        super().__init__()
  
        # setting title
        self.setWindowTitle("Python ")
  
        # setting geometry
        self.setGeometry(100, 100, 600, 400)
  
        # calling method
        self.UiComponents()
  
        # showing all the widgets
        self.show()
  
        # method for widgets
    def UiComponents(self):
  
        # creating spin box
        self.spin = QSpinBox(self)
  
        # setting geometry to spin box
        self.spin.setGeometry(100, 100, 150, 40)
  
        # setting range to the spin box
        self.spin.setRange(-1, 11)
  
        # setting prefix to spin
        self.spin.setPrefix("Value : ")
  
        # add action to this spin box
        self.spin.valueChanged.connect(self.action_spin)
  
  
    # method called after editing finished
    def action_spin(self):
  
        # getting current value of spin box
        current = self.spin.value()
  
        # checking if current value is minimum
        if current == -1:
  
            # setting spin box value to maximum - 1
            self.spin.setValue(10)
  
        # checking if current value is maximum
        elif current == 11:
  
            # setting spin box value to minimum + 1
            self.spin.setValue(0)
  
  
# create pyqt5 app
App = QApplication(sys.argv)
  
# create the instance of our Window
window = Window()
  
# start the app
sys.exit(App.exec())

chevron_right


Output :

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :