使用PyQt5使Q循环窗口小部件在for循环中可区分

时间:2018-02-18 16:38:27

标签: python pyqt pyqt5 qcombobox

我在使用创建QComboBox小部件网格的应用程序时遇到问题(见下图)。我使用for循环来创建网格的元素。

QComboBox网格布局

我希望能够单独处理每个QComboBox。以下是没有尝试这样做的代码:

grid = QGridLayout()
combos = [
'1', '1', '1', '', '1', 
'1', '1', '1', '', '1',
'1', '1', '1', '', '1',
'1', '1', '1', '', '1']

positions = [(i,j) for i in range(5) for j in range(5)]
for position, dcombo in zip(positions, combos):
    if dcombo == '':
        continue
    combo = QComboBox()

    for x in range(0, 30):
        combo.addItem(QIcon("/icons/%d.PNG" % x),"")

    combo.setFixedSize(120,100)
    combo.setIconSize(QSize(100,100))
    grid.addWidget(combo, *position)

    comboList['combo{0}'.format(position)] = position

这是我的尝试,以及我目前被困的地方:

grid = QGridLayout()
combos = [
'1', '1', '1', '', '1', 
'1', '1', '1', '', '1',
'1', '1', '1', '', '1',
'1', '1', '1', '', '1']

comboList = {}

positions = [(i,j) for i in range(5) for j in range(5)]
for position, drawcombo in zip(positions, combos):
    if drawcombo == '':
        continue
    combo = QComboBox()

    for x in range(0, 30): #
        combo.addItem(QIcon("absolver deck reviewer/icons/%d.PNG" % x),"")

    combo.setFixedSize(120,100)
    combo.setIconSize(QSize(100,100))
    grid.addWidget(combo, *position)

    comboList['combo{0}'.format(position)] = position
    combo.currentIndexChanged.connect(lambda: self.logChange(comboList['combo{0}'.format(position)]))

def logChange(self, currentCombo):
    sender = self.sender()
    print(str(currentCombo) + ' was changed')

print()方法只返回列表中的最后一个位置(在本例中为(3,4)元组。

1 个答案:

答案 0 :(得分:2)

当位置变量发生变化时,它会被覆盖,只打印最后一个变量,如果你不希望它被覆盖,你必须将它作为参数传递给lambda函数。为此,您还必须将发送信号的变量作为参数传递,在您的情况下使用以下内容:

page