📜  PyQt5 StringSpinBox – 循环字符串

📅  最后修改于: 2022-05-13 01:55:08.188000             🧑  作者: Mango

PyQt5 StringSpinBox – 循环字符串

在本文中,我们将看到如何使字符串值在最后一个值达到时重复自己应该回到第一个值,类似地,当值在第一个值之后递减时它应该回到最后一个值。为此,我们必须更改 StringSpinBox 的自定义类代码。

下面是自定义 StringSpinBox 类的代码

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

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

        # list of strings
        strings = ["a", "b", "c", "d", "e", "f", "g"]


        # calling set Strings method with
        # adding blank value in front and in end
        self.setStrings(["BLANK"] + strings + ["BLANK"])

        # show first value from index 1
        self.setValue(1)

    def reset_spin(self):
        if self.value() == len(self.strings()) - 1:
            self.setValue(0)

    # 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]

    # overwriting the stepBy method
    # method that get called when values get incremented or decremented
    def stepBy(self, step):

        # checking if current value is 1 and step is -1
        # step = -1 means decrement
        if self.value() == 1 and step == -1:

            # set current value to maximum
            self.setValue(self.maximum())

        # checking if current value is maximum -1 and step is 1
        # step = 1 means Increment
        elif self.value() == self.maximum() - 1 and step == 1:

            # setting current value to 0
            self.setValue(0)
        
        # calling stepBy method of QSpinBox
        QSpinBox.stepBy(self, step)

说明:在字符串微调框自定义类中覆盖 stepBy 方法,该方法在发生增量或减量时调用,检查是否存在减量并且当前值是最小值,然后如果存在增量并且当前值类似,则将其值更改为最大值在最大值然后将其值更改为最小值 -1 值。

下面是实现

# 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)
  
        # list of strings
        strings = ["a", "b", "c", "d", "e", "f", "g"]
  
  
        # calling set Strings method with
        # adding blank value in front and in end
        self.setStrings(["BLANK"] + strings + ["BLANK"])
  
        # show first value from index 1
        self.setValue(1)
  
    def reset_spin(self):
        if self.value() == len(self.strings()) - 1:
            self.setValue(0)
  
    # 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]
  
    # overwriting the stepBy method
    # method that get called when values incremented
    def stepBy(self, step):
  
        # checking if current value is 1 and step is -1
        # step = -1 means decrement
        if self.value() == 1 and step == -1:
  
            # set current value to maximum
            self.setValue(self.maximum())
  
        # checking if current value is maximum -1 and step is 1
        # step = 1 means Increment
        elif self.value() == self.maximum() - 1 and step == 1:
  
            # setting current value to 0
            self.setValue(0)
  
        # calling stepBy method by QSpinBox
        QSpinBox.stepBy(self, step)
  
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())

输出 :