from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * import sys import os class SelectMapWindow(QMainWindow): def __init__(self): QMainWindow.__init__(self) w = QWidget() self.setCentralWidget(w) self.topFiller = QWidget() self.topFiller.setMinimumSize(250, len(os.listdir("source/map"))*50+30) self.mainGroupLayOut() scroll = QScrollArea() scroll.setWidget(self.topFiller) vbox = QVBoxLayout() vbox.addWidget(scroll) w.setLayout(vbox) self.resize(300, 500) def mainGroupLayOut(self): number = 0 self.varList = os.listdir("source/map") for filename in os.listdir("source/map"): self.varList[number] = QPushButton(self.topFiller) self.varList[number].setText(str(filename)) self.varList[number].clicked.connect(lambda: self.MapButton_clicked(self.sender().text()))#########连接槽函数并把自身变量名传递过去 self.varList[number].move(10,(number+1)*50) number += 1 def MapButton_clicked(self,number): print(number) if __name__ == '__main__': app = QApplication(sys.argv) window = SelectMapWindow() window.show() sys.exit(app.exec_())