使用 PyQt5 使 QComboBox 小部件在 for 循环中可区分
Making QComboBox widgets distinguishable in a for loop using PyQt5
我在使用创建 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() 方法仅 returns 列表中的最后一个位置(在本例中为 (3, 4) 元组。
随着位置变量的改变,它被覆盖,它只打印最后一个,如果你不想它被覆盖,你必须将它作为参数传递给 lambda 函数,此外。为此,您还必须将发送信号的变量作为参数传递,在您的情况下使用以下内容:
combo.currentIndexChanged.connect(
lambda ix, p=position: self.logChange(comboList['combo{0}'.format(p)]))
我在使用创建 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() 方法仅 returns 列表中的最后一个位置(在本例中为 (3, 4) 元组。
随着位置变量的改变,它被覆盖,它只打印最后一个,如果你不想它被覆盖,你必须将它作为参数传递给 lambda 函数,此外。为此,您还必须将发送信号的变量作为参数传递,在您的情况下使用以下内容:
combo.currentIndexChanged.connect(
lambda ix, p=position: self.logChange(comboList['combo{0}'.format(p)]))