如何在没有列表或数组的循环中更改变量名称 - Python

时间:2015-12-17 21:37:58

标签: python loops label pyqt5

如何在循环中更改变量的名称,而不使用列表或数组。我正在使用pyqt5处理dekstop应用程序。

我有图像文件的标签,例如:image1,image2,image3 ......

 def click_facecrop(self):
    shutil.copy2(filename, './u_image.jpg')
    myImage = SImage('u_image.jpg')
    myImage.face_multiple_crop_from_image('testcrop')
    files = glob.glob("./testcrop/*.jpg")
    for file in files:
        try:
            print str(file)
            p_map = QPixmap(file)

            #this image1 must change in while loop like image2,image3...
            self.image1.setScaledContents(True)
            self.image1.setPixmap(p_map)
        except:
            pass

1 个答案:

答案 0 :(得分:0)

如果要在self上获取属性,可以使用函数getattr

def click_facecrop(self):
    shutil.copy2(filename, './u_image.jpg')
    myImage = SImage('u_image.jpg')
    myImage.face_multiple_crop_from_image('testcrop')
    files = glob.glob("./testcrop/*.jpg")
    for filename in files:
        try:
            print str(filename)
            p_map = QPixmap(filename)
        #this image1 must change in while loop like image2,image3...
        getattr(self, filename).setScaledContents(True)
        getattr(self, filename).setPixmap(p_map)
    except:
        pass