使用PyQt5使qcombox小部件在for循环中可区分

2024-09-30 03:25:27 发布

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

我在一个创建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)元组)。你知道吗


Tags: inselfformat网格forpositionrangegrid

热门问题