PyQT - 多个组合框,每个函数参数一个

PyQT - Multiple ComboBoxes, One for Each Function Argument

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

目前,两个连接 return a 'multiply() takes exactly 2 arguments (1 given)" 错误,因为两个组合框不是同时而是分别连接到函数。

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_())

因此,首先,我建议不要在 setupUI 中定义函数,而是让您想要使用 QMainWindow 的 children/attributes 的小部件。然后您可以在 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

希望这就是您要找的。

您面临的问题是,您没有从同一事件中获得将两者相乘的值。

解决方案是使用一个函数,该函数在每次更改时都会被调用,并聚合要从 table 中的两个(或更多)项目处理的值。 因此,您从 table 中调用 "pulls" 函数而不是 "sending" 值。为此,table 当然需要从函数外部可见,这可以通过将其设为 class 属性来实现。

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_())