可能是这样的:
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
class MyWin(QMainWindow):
def __init__(self):
super().__init__()
centralWidget = QWidget()
self.setCentralWidget(centralWidget)
self.rowId = 777 # <---
self.listWidget = QListWidget()
self.listWidget.itemClicked.connect(
lambda it, rowId=self.rowId : self.listWidgetClicked(it, rowId)) # <---
self.layout = QVBoxLayout(centralWidget)
self.createListWidget()
def createListWidget(self):
for it in ["item1", "item2", "item3"]:
self.listWidget.addItem(QListWidgetItem(it))
self.layout.addWidget(self.listWidget)
@pyqtSlot()
def listWidgetClicked(self, item, rowId ): # <---
print('click -> {}, rowId=`{}`'.format(item.text(), rowId))
if __name__=="__main__":
import sys
app = QApplication(sys.argv)
myapp = MyWin()
myapp.show()
sys.exit(app.exec_())