如何在没有列表或数组的情况下更改循环中的变量名

2024-04-26 13:14:51 发布

您现在位置:Python中文网/ 问答频道 /正文

如何在循环中更改变量名而不使用列表或数组。我正在使用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

Tags: inimageselfmap列表files数组glob
1条回答
网友
1楼 · 发布于 2024-04-26 13:14:51

如果要在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

相关问题 更多 >