我正在使用动画类在标签上显示spritesheet作为gif(代码在这里:http://python.su/forum/topic/15679/?page=1#post-94136)。为了在PyQt应用程序上执行此操作,我在不同的线程上运行,通过使用信号进行通信,一旦我不想停止主线程。
但是当我点击label_2时,我收到以下错误:
File "C:\Users\Alexandre\Desktop\breakingthread\animation.py", line 21, in _animation_step
self.label.setPixmap(self.pixmaps[self._current_frame])
IndexError: list index out of range
知道如何解决?
以下完整代码:
mainwindow.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'bt.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(473, 700)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
MainWindow.setSizePolicy(sizePolicy)
MainWindow.setMinimumSize(QtCore.QSize(473, 700))
MainWindow.setMaximumSize(QtCore.QSize(473, 700))
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.label = QtGui.QLabel(self.centralwidget)
self.label.setGeometry(0, 0, 473, 700)
self.label.setText(_fromUtf8(""))
self.label.setPixmap(QtGui.QPixmap(_fromUtf8("images/bg.png")))
self.label.setObjectName(_fromUtf8("label"))
self.label_2 = QtGui.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(100, 610, 286, 42))
self.label_2.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.label_2.setText(_fromUtf8(""))
self.label_2.setPixmap(QtGui.QPixmap(_fromUtf8("images/click.png")))
self.label_2.setObjectName(_fromUtf8("label_2"))
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "Breaking Thread", None))
runner.py
from PyQt4 import QtGui
from PyQt4.QtCore import QThread, SIGNAL
from animation import SpriteAnimation
import sys
import mainwindow
class animateLabel(QThread):
def __init__(self):
QThread.__init__(self)
def __del__(self):
self.wait()
def animate_label(self):
print "inicio thread"
def run(self):
self.animate_label()
self.emit(SIGNAL('init_animate()'))
class App(QtGui.QMainWindow, mainwindow.Ui_MainWindow):
def __init__(self):
super(self.__class__, self).__init__()
self.setupUi(self)
self.label_2.mousePressEvent = self.call_animation
def call_animation(self, event):
self.get_thread = animateLabel()
self.connect(self.get_thread, SIGNAL("finished()"), self.done)
self.connect(self.get_thread, SIGNAL("init_animate()"), self.init_animate)
self.get_thread.start()
def init_animate(self):
self.animation = SpriteAnimation('image/click', 286, 42, self.label_2)
self.animation.play()
def done(self):
print "fim thread"
def main():
app = QtGui.QApplication(sys.argv)
form = App()
form.show()
app.exec_()
if __name__ == '__main__':
main()