PyQT多个组合框,每个函数Argumen一个

2024-04-23 20:43:16 发布

您现在位置:Python中文网/ 问答频道 /正文

在下面的示例中,我尝试使用两个独立组合框的选择作为函数的两个参数。如果选择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_())

Tags: 函数inimportuifor参数systable
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

希望这就是你要找的。在

你面临的问题是你不能从同一个事件中得到两个值的乘积。在

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

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

相关问题 更多 >