在下面的示例中,我尝试使用两个独立组合框的选择作为函数的两个参数。如果选择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_())
因此,首先,我建议不要在
setupUI
中定义函数,并使您想要使用的小部件使用QMainWindow的子级/属性。然后您可以在multiply
方法中访问它们。对于你的回答,我想做以下几点:希望这就是你要找的。在
你面临的问题是你不能从同一个事件中得到两个值的乘积。在
解决方案是使用一个函数,该函数在每次更改时都会被调用,并将表中两个(或多个)项中要处理的值聚合在一起。 因此,您调用的函数不是“发送”值,而是从该表中“提取”值。为此,表当然需要从函数外部可见,这可以通过将其设为类属性来实现。在
相关问题 更多 >
编程相关推荐