1 # coding=utf-8 2 ''' 3 Created by Alpha on 2017-05-25. 4 Copyright (c) 2018 Alpha.All rights reserved. 5 ''' 6 try: 7 from PyQt4 import QtGui, QtCore 8 from PyQt4.QtCore import pyqtSignal as pyqtSignal 9 from PyQt4.QtCore import pyqtSlot as pyqtSlot 10 except: 11 pass 12 import os,sys 13 import math 14 #usage first 15 class myLabel(QtGui.QLabel): 16 Clicked = pyqtSignal(str) 17 def __init__(self,parent=None): 18 super(myLabel, self).__init__(parent) 19 self.textStr = '' 20 def mousePressEvent(self, e): 21 self.Clicked.emit(self.textStr) 22 23 class myTest(QtGui.QWidget): 24 def __init__(self,parent=None): 25 super(myTest, self).__init__(parent) 26 27 self.imgPath = [] 28 self.callbackList = [] 29 self.imgLabels = [] 30 31 self.grideLayout = QtGui.QGridLayout() 32 self.setLayout(self.grideLayout) 33 34 def loadImg(self,callback,*args): 35 self.callbackList.append(callback) 36 if not os.path.isdir(args): 37 return False 38 dirs = os.listdir(args) 39 for file in dirs: 40 imgLabel = myLabel() 41 self.imgPath.append(file) 42 self.imgLabels.append(imgLabel) 43 indexImg = dirs.index(file) 44 imgLabel.Clicked.connect(self.callbackList[indexImg])