简体   繁体   中英

Showing Consecutive images on a QLabel PyQt4

I'm trying to show consecutive images on a QLabel the images are numbered from 0000000 to 0000199 the problem is that the num variable prints empty string

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_()

Please help

len(str(i)) - 7 returns a negative number. You need to swap it around:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM