在QLabel PyQt4上显示连续图像

时间:2017-09-07 07:44:23

标签: python python-2.7 pyqt pyqt4 qpixmap

我试图在QLabel上显示连续的图像 图像编号从0000000到0000199 问题是num变量打印空字符串

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys, time

class Animation(QMainWindow):
    def __init__(self, parent=None):
        super(Animation, self).__init__(parent)

        self.resize(QSize(720, 480))

        self.imageViewer = QLabel(self)
        self.setCentralWidget(self.imageViewer)

        startBtn = QPushButton("start", self)
        startBtn.clicked.connect(self.start)
        self.statusBar().addWidget(startBtn)

    def start(self):
        i = 0
        while 1:
            num = ("0" * (len(str(i)) - 7)) + str(i)
            name = "frame" + num + ".png"
            print ("0" * (len(str(i)) - 7))
            self.imageViewer.setPixmap(QPixmap(name))
            if i == 199:
                break
            i += 1
            time.sleep(1)

app = QApplication(sys.argv)
test = Animation()
test.show()
app.exec_()

请帮忙

1 个答案:

答案 0 :(得分:0)

len(str(i)) - 7会返回一个负数。你需要交换它:

num = '0' * (7-len(str(i))) + str(i)