【问题标题】:(PyQT) How can I make sure values in all following spinboxes are higher than the last(PyQT)如何确保所有以下旋转框中的值都高于上一个
【发布时间】:2016-09-03 18:06:34
【问题描述】:

以下是可能添加的输入示例。

值 0:[     0 ] [ 100 ]
值 1:[ 200 ] [ 300 ]
值 2:[ 400 ] [ 500 ]

以上是正确的

值 0:[     0 ] [ 800 ]
值 1:[ 700 ] [ 600 ]
值 2:[ 500 ] [ 400 ]

以上不正确:确保每个值都高于上一个

下面的代码将用户输入的每个值添加到数组中。

def val_norm(self, MainWindow, count):
    self.verticalLayout_2 = QtGui.QVBoxLayout()
    self.verticalLayout_2.setMargin(11)
    self.verticalLayout_2.setSpacing(6)
    self.verticalLayout_2.setObjectName(_fromUtf8("verticalLayout_2"))
    self.horizontalLayout_2 = QtGui.QHBoxLayout()
    self.horizontalLayout_2.setMargin(11)
    self.horizontalLayout_2.setSpacing(6)
    self.horizontalLayout_2.setObjectName(_fromUtf8("horizontalLayout_2"))
    self.norm_label = QtGui.QLabel(self.Normalization)
    self.norm_label.setObjectName(_fromUtf8("norm_label"))
    self.horizontalLayout_2.addWidget(self.norm_label)

    self.norm_spinBox_[(count*2)-1] = QtGui.QSpinBox(self.Normalization)    # All the left side boxes
    self.norm_spinBox_[(count*2)] = QtGui.QSpinBox(self.Normalization)      # All the right side boxes

    self.norm_spinBox_[(count*2)-1].setMaximum(1000)                        # set the maximums for the left side
    self.norm_spinBox_[(count*2)].setMaximum(1000)                          # set the maximums for the right side
    self.horizontalLayout_2.addWidget(self.norm_spinBox_[(count*2)-1])      # adding the actual object to UI (left)
    self.horizontalLayout_2.addWidget(self.norm_spinBox_[(count*2)])        # adding the actual object to UI (right)
    self.verticalLayout_2.addLayout(self.horizontalLayout_2)                # setting up layout
    self.verticalLayout.addLayout(self.verticalLayout_2)                    # setting up layout


    # for debugging purposes
    self.norm_spinBox_[(count*2)-1].editingFinished.connect(lambda: print(self.norm_spinBox_[(count*2)-1].text()))
    self.norm_spinBox_[(count*2)].editingFinished.connect(lambda: print(self.norm_spinBox_[(count*2)].text()))

    self.norm_label.setText(_translate("MainWindow", "Value {}".format(self.count), None))
    self.count += 1


    if self.norm_spinBox_[(count*2)-1].text() > self.norm_spinBox_[count*2].text():
        print("That's wrong!")

我认为使用 if 语句会起作用,但我显然误会了我应该如何处理这个问题。如果我错了,请纠正我,但我认为它不起作用,因为我没有在编辑后将 if 语句连接到每个框。

if self.norm_spinBox_[(count*2)-1].text() > self.norm_spinBox_[count*2].text():
    print("That's wrong!")

【问题讨论】:

    标签: python pyqt


    【解决方案1】:

    您可以根据前一个 spinbox 的值使用以下 spinbox 的 QSpinBoxvalueChanged() 信号到 setMinimum(),因此 spinbox 只接受值 >= minimum,这里是一个工作示例:

    import sys
    from PyQt5.QtCore import *
    from PyQt5.QtGui import *
    from PyQt5.QtWidgets import *
    
    class SpinboxWidget(QWidget):
        def __init__(self, parent=None):
            QWidget.__init__(self, parent)
            self.layout = QVBoxLayout()
            # self.signalMapper = QSignalMapper(self)                   
            # self.signalMapper.mapped[str].connect(self.setspbMin)
            self.addSpinboxes(10)                                   # add an arbitrary number of spinboxes
            self.setLayout(self.layout)
    
        def addSpinboxes(self, n):
            spb = []
            for i in range(n):
                # objectname = 'spinbox_{}'.format(i)
                spinbox = QSpinBox()
                # spinbox.setObjectName(objectname)                 # to identify the spinbox later
                spinbox.setMaximum(100)
                # spinbox.valueChanged.connect(self.signalMapper.map)
                # self.signalMapper.setMapping(spinbox, objectname) # sends the objectname with his mapped() signal
                spb.append(spinbox)                                 # added in edit
                self.layout.addWidget(spinbox)
    
            for i in range(n-1):
                spb[i].valueChanged.connect(spb[i + 1].setMinimum)
    
        '''            
        def setspbMin(self, identifier):       
            spinbox = self.findChild(QSpinBox,identifier)           # don't use QObject.sender() see Documentation
            nextIndex = int(identifier.lstrip('spinbox_')) + 1
            nextIdentifier = 'spinbox_{}'.format(nextIndex)
            nextSpinbox = self.findChild(QSpinBox,nextIdentifier)
            try:
                nextSpinbox.setMinimum(spinbox.value()) 
            except AttributeError:
                pass
        '''
    
    if __name__ == '__main__':  
        app = QApplication(sys.argv)
        app.setStyle('plastique')
        widget = SpinboxWidget()
        widget.setWindowTitle('Spinbox Tester')
        widget.show()
    
    sys.exit(app.exec_())
    

    编辑: 正如 ekhumoro signalMapper 所建议的那样,不需要 -> 不再需要注释掉的行。 valueChanged-信号连接到以下 spinbox 的 setMinimum()

    【讨论】:

    • 这看起来是一个不错的解决方案,但不需要信号映射器。在循环中,只需将前一个 spinbox 的valueChanged 信号连接到当前 spinbox 的setMinimum 插槽即可。如果你这样做,SpinboxWidget 类可以减少到大约 10 行代码。
    • 确实,它在没有 signalMapper 的情况下工作。我将按照建议编辑我的代码
    • 谢谢 a_manthey,这是一种非常聪明的做法
    猜你喜欢
    • 1970-01-01
    • 2017-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-08
    • 2017-05-20
    • 2021-11-23
    • 2023-02-21
    相关资源
    最近更新 更多