Python 如何为循环中创建的QPushButtons分配键

Python 如何为循环中创建的QPushButtons分配键,python,for-loop,pyqt5,key-bindings,qpushbutton,Python,For Loop,Pyqt5,Key Bindings,Qpushbutton,使用PYQT5,我使用QPushButton创建了一个按钮网格,如下所示: self.pads = [] self.binding = ['a','b','c','d','e','f','g','h','i'] #add quota for binding for r in range(3): for c in range(3): pad = QPushButton() pad

使用PYQT5,我使用QPushButton创建了一个按钮网格,如下所示:

self.pads = []
        self.binding = ['a','b','c','d','e','f','g','h','i'] #add quota for binding
        for r in range(3):
            for c in range(3):
                pad = QPushButton()
                pad.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
                pad.setStyleSheet("QPushButton { background-color: lightcoral }"
                        "QPushButton:Hover { background-color: lightpink }"
                      "QPushButton:pressed { background-color: indianred }" )
                self.pads.append(pad)
                padLayout.addWidget(pad, r, c)
                padLayout.setSpacing(30)
        for i in self.pads:
            i.setShortcut(self.binding)
我还创建了一个按键数组,我想我可以使用该数组使用setShortcut,这样当我按下一个键时,它只会触发其中一个按钮。当我使用循环创建按钮时,我很难理解如何实现此功能

此外,当我尝试此操作时,会出现以下错误:

Traceback (most recent call last):
  File "/Users/ricochetnkweso/PycharmProjects/pythonProject/Crash_Test.py", line 160, in <module>
    window = mainWindow()  # After Qapp instance but before event loop
  File "/Users/ricochetnkweso/PycharmProjects/pythonProject/Crash_Test.py", line 60, in __init__
    i.setShortcut(self.binding)
TypeError: setShortcut(self, Union[QKeySequence, QKeySequence.StandardKey, str, int]): argument 1 has unexpected type 'list'
回溯(最近一次呼叫最后一次):
文件“/Users/ricochetnkweso/PycharmProjects/pythonProject/Crash_Test.py”,第160行,在
window=mainWindow()#在Qapp实例之后但在事件循环之前
文件“/Users/ricochetnkweso/PycharmProjects/pythonProject/Crash_Test.py”,第60行,在__
i、 设置短切(自绑定)
TypeError:setShortcut(self,Union[QKeySequence,QKeySequence.StandardKey,str,int]):参数1具有意外的类型“list”

我不明白它的意思。

使用
zip
功能:

for button, shortcut in zip(self.pads, self.binding):
    button.setShortcut(shortcut)

正如错误明确指出的那样,您不能将字符串列表(这就是
self.binding
是什么)设置为快捷方式,因为
setShortcut
需要QKeySequence、QKeySequence.StandardKey、str或int(QtCire.Qt.Key枚举)中的任何一个作为参数。