我正在使用PyQt5在电子邮件客户端上工作,我需要在创建对象时将对象的索引传递到列表中(程序中的实际代码):
self.searchMessageList = [] for message in self.searchQuery()[0:50]: self.searchMessageList.append(ClickableMessageBox(self, instance.getMessage('me', message['id']), 'grey', (0, YPos), SMLObj, SMLIndex, self.testFunc)) YPos = YPos + 40 print('done')
问题是当我将SMLIndex作为参数传递时,ClickableMessageBox
类(以下代码块)给了我这个错误:
self.searchMessageList.append(ClickableMessageBox(self, instance.getMessage('me', message['id']), 'grey', (0, YPos), self.searchMessageList, currentIndex, self.testFunc)) File "main.py", line 107, in __init__ self.msgLabel.mouseReleaseEvent = event(self.SMLObj[self.SMLIndex]) IndexError: list index out of range
据我所知,ClickableMessageBox
的实例尚未添加到列表中-我只是找不到解决此问题的方法。
处理标签的消息框类:
class ClickableMessageBox(QtWidgets.QWidget): def __init__(self, object, text, colour, coords, SMLObj, SMLIndex, event): super().__init__() self.msgLabel = QtWidgets.QLabel(object) self.msgLabel.setText(text) self.msgLabel.setStyleSheet("color: {}".format(colour)) self.msgLabel.move(coords[0], coords[1]) self.SMLObj = SMLObj self.SMLIndex = SMLIndex self.msgLabel.mouseReleaseEvent = event(self.SMLObj[self.SMLIndex])
self.testFunc
是
def testFunc(self, event, object): object.msgLabel.setStyleSheet("color: white") print('Clicked!')
一如既往,任何帮助将不胜感激。