【问题标题】:PyQT - Multiple ComboBoxes, One for Each Function ArgumentPyQT - 多个组合框,每个函数参数一个
【发布时间】:2017-09-01 04:54:32
【问题描述】:

在下面的示例中,我尝试将两个单独的组合框的选择用作函数的两个参数。如果选择 3 和 4,则应产生 12 的输出,依此类推。如何编写此代码以将第一个组合选择作为第一个参数发送,将第二个组合选择作为第二个参数发送?

目前,两个连接都返回“multiply() 恰好需要 2 个参数(1 个给定)”错误,因为这两个组合框不是同时而是分别连接到函数。

from PyQt4 import QtCore, QtGui

class Ui_MainWindow(QtGui.QMainWindow):

    def setupUi(self):

        window = QtGui.QMainWindow(self)
        window.table = QtGui.QTableWidget()
        window.table.setRowCount(2)
        window.table.setColumnCount(1)
        window.setCentralWidget(window.table)

        def multiply(x, y):
            return x * y

        combo_x = QtGui.QComboBox()
        combo_y = QtGui.QComboBox()
        for i in range(1, 10):
            combo_x.addItem(str(i))
            combo_y.addItem(str(i))

        combo_x.activated[int].connect(multiply)
        combo_y.activated[int].connect(multiply)

        window.table.setCellWidget(0, 0, combo_x)
        window.table.setCellWidget(1, 0, combo_y)

        desired = []
        for x in range(1, 10):
            for y in range(1, 10):
                desired.append(multiply(x, y))

        window.show()

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    ui = Ui_MainWindow()
    ui.setupUi()
    sys.exit(app.exec_())

【问题讨论】:

    标签: python combobox pyqt pyqt4 qcombobox


    【解决方案1】:

    您面临的问题是您无法从同一个事件中获得将两者相乘的值。

    解决方案是使用一个函数,该函数在每次更改时都被调用,并从表中的两个(或更多)项中聚合要处理的值。 因此,您调用的函数不是“发送”值,而是从该表中“提取”它们。为此,表格当然需要从函数外部可见,这可以通过将其设为类属性来完成。

    from PyQt4 import QtGui
    
    class Ui_MainWindow(QtGui.QMainWindow):
    
        def setupUi(self):
            self.table = QtGui.QTableWidget()
            self.table.setRowCount(3)
            self.table.setColumnCount(1)
            self.setCentralWidget(self.table)
    
            combo_x = QtGui.QComboBox()
            combo_y = QtGui.QComboBox()
            for i in range(1, 10):
                combo_x.addItem(str(i))
                combo_y.addItem(str(i ** 2))
    
            combo_x.activated.connect(self.update)
            combo_y.activated.connect(self.update)
    
            self.table.setCellWidget(0, 0, combo_x)
            self.table.setCellWidget(1, 0, combo_y)
            self.table.setCellWidget(2,0,QtGui.QLabel(""))
    
            self.show()
    
        def multiply(self,x,y):
            return x*y
    
        def update(self):
            x = self.table.cellWidget(0, 0).currentText() #row, col
            y = self.table.cellWidget(1, 0).currentText()
            result = self.multiply(int(x), int(y))
            self.table.cellWidget(2, 0).setText(str(result))
    
    if __name__ == "__main__":
        import sys
        app = QtGui.QApplication(sys.argv)
        ui = Ui_MainWindow()
        ui.setupUi()
        sys.exit(app.exec_())
    

    【讨论】:

      【解决方案2】:

      因此,首先,我建议不要在setupUI 中定义函数,并让您想要使用的小部件使用 QMainWindow 的子级/属性。然后你可以在你的multiply 方法中访问它们。特别是对于您的回答,我将执行以下操作:

      class Ui_MainWindow(QtGui.QMainWindow):
      
          def setupUi(self):
      
              # No Changes made here
              window = QtGui.QMainWindow(self)
              window.table = QtGui.QTableWidget()
              window.table.setRowCount(2)
              window.table.setColumnCount(1)
              window.setCentralWidget(window.table)
      
              # Make attribute of MainWindow
              self.combo_x = QtGui.QComboBox()
              self.combo_y = QtGui.QComboBox()
              for i in range(1, 10):
                  self.combo_x.addItem(str(i))
                  self.combo_y.addItem(str(i))
      
              self.combo_x.activated[int].connect(self.multiply)
              self.combo_y.activated[int].connect(self.multiply)
      
              window.table.setCellWidget(0, 0, self.combo_x)
              window.table.setCellWidget(1, 0, self.combo_y)
      
              window.show()
      
          def multiply(self):
      
              # Grab Index of comboboxes
              x = int(self.combo_x.currentIndex())+1
              y = int(self.combo_y.currentIndex())+1
      
              # Multiply
              print x * y
      

      希望这就是你要找的。​​p>

      【讨论】:

      • 这几乎适用于建议的编辑,但在任一组合框中选择 n 会导致 (n - 1) 用于计算(即“1 x 1”= 0、“2 x 2”= 1 , "3 x 3" = 4)。我试图通过将 x 和 y 设置为 int(self.combo_x or y.currentIndex() + 1) 来修复它,但这导致了奇怪的事情发生。
      • 那是因为python中的索引从0开始。我在代码中修复了它。
      • 这确实有效,但它似乎更像是一种解决方法。它实际上并没有通过函数使用组合框的内容。在这种情况下就足够了,但在我的实际情况下,选择是文本,所以 currentIndex 不是应该使用的。
      • 抱歉,我以为你想要索引。要获取“内容”或文本,您可以使用int(self.combo_x.currentText())
      猜你喜欢
      • 1970-01-01
      • 2021-05-22
      • 2012-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-20
      • 2018-09-27
      相关资源
      最近更新 更多