Skip to content
Related Articles

Related Articles

PyQt5 – Creating String Spin Box
  • Last Updated : 17 May, 2020

In this article we will see how we can create a spin box which have string values, by default the spin box contains only integer values. String Spin box is a spin box which have string values to select from the given string values.

In order to create a String Spin Box we have create a custom class which inherits the QSpinBox class, below is the String Spin Box class syntax

# custom class for String Spin Box
class StringBox(QSpinBox):

    # constructor
    def __init__(self, parent=None):
        super(StringBox, self).__init__(parent)

        # string values
        strings = ["a", "b", "c", "d", "e", "f", "g"]

        # calling setStrings method
        self.setStrings(strings)

    # method setString
    # similar to set value method
    def setStrings(self, strings):

        # making strings list
        strings = list(strings)

        # making tuple from the string list
        self._strings = tuple(strings)

        # creating a dictionary
        self._values = dict(zip(strings, range(len(strings))))

        # setting range to it the spin box
        self.setRange(0, len(strings)-1)

    # overwriting the textFromValue method
    def textFromValue(self, value):
        
        # returning string from index
        # _string = tuple
        return self._strings[value]

Explanation : The basic idea of this class is that we create a custom class which inherits the spin box class that’s why it has all the abilities of normal spin box but instead of displaying integer it display the string value. We have created dictionary such that for each string it has a integer key and the range of spin box is the number of string values and with the help of overwriting the textFromValue method it shows string instead of the integer.

Below is the implementation




# importing libraries
from PyQt5.QtWidgets import * 
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import * 
from PyQt5.QtCore import * 
import sys
  
# custom class for String Spin Box
class StringBox(QSpinBox):
  
    # constructor
    def __init__(self, parent = None):
        super(StringBox, self).__init__(parent)
  
        # string values
        strings = ["a", "b", "c", "d", "e", "f", "g"]
  
        # calling setStrings method
        self.setStrings(strings)
  
    # method setString
    # similar to set value method
    def setStrings(self, strings):
  
        # making strings list
        strings = list(strings)
  
        # making tuple from the string list
        self._strings = tuple(strings)
  
        # creating a dictionary
        self._values = dict(zip(strings, range(len(strings))))
  
        # setting range to it the spin box
        self.setRange(0, len(strings)-1)
  
    # overwriting the textFromValue method
    def textFromValue(self, value):
  
        # returning string from index
        # _string = tuple
        return self._strings[value]
  
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 a string spin box
        string_spin_box = StringBox(self)
  
        # setting geometry to the spin box
        string_spin_box.setGeometry(100, 100, 200, 40)
  
  
# create pyqt5 app
App = QApplication(sys.argv)
  
# create the instance of our Window
window = Window()
  
# start the app
sys.exit(App.exec())

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 :