PyQt:QWidget 幻灯片图像未在 QMainWindow 中正确显示
PyQt: QWidget slideshow images not showing up correctly in QMainWindow
我创建了两个 classes - 从 QtDesigner 生成的 QMainWindow 和单独执行时效果完美的 QWidget 幻灯片。我设法将小部件合并到 MainWindow class 中,它执行得很好但是当我单击幻灯片按钮时,图像没有显示并出现此错误:
line 61, in timerEvent
if self.step >= len(self.image_files):
TypeError: object of type 'MainWindow' has no len()
这可能是一个小错误,但我对 PyQt 还是个新手,我已经尝试摆弄它差不多一天了,哈哈。一如既往,我们将不胜感激!
完整代码如下:
from PyQt4 import QtCore, QtGui
import sys
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 MainWindow(QtGui.QMainWindow):
#(self, parent=None) <- original code
def __init__(self, image_files, parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.setupUi(self)
#Initialized Widget here
self.slides_widget = Slides(self)
self.setCentralWidget(self.slides_widget)
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(1012, 532)
self.tabWidget = QtGui.QTabWidget(Form)
self.tabWidget.setGeometry(QtCore.QRect(470, 130, 451, 301))
self.tabWidget.setObjectName(_fromUtf8("tabWidget"))
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "Form", None))
class Slides(QtGui.QWidget):
def __init__(self, image_files, parent=None):
QtGui.QWidget.__init__(self, parent)
self.image_files = image_files
self.label = QtGui.QLabel("", self)
self.label.setGeometry(50, 150, 450, 350)
#button
self.button = QtGui.QPushButton(". . .", self)
self.button.setGeometry(200, 100, 140, 30)
self.button.clicked.connect(self.timerEvent)
self.timer = QtCore.QBasicTimer()
self.step = 0
self.delay = 3000 #ms
sTitle = "DIT Erasmus Page : {} seconds"
self.setWindowTitle(sTitle.format(self.delay/1000.0))
def timerEvent(self, e=None):
if self.step >= len(self.image_files):
self.timer.start(self.delay, self)
self.step = 0
return
self.timer.start(self.delay, self)
file = self.image_files[self.step]
image = QPixmap(file)
self.label.setPixmap(image)
self.setWindowTitle("{} --> {}".format(str(self.step), file))
self.step += 1
image_files = ["slide1.jpg", "slide2.jpg", "slide3.jpg", "slide4.jpg"]
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
Form = MainWindow(image_files)
ui = MainWindow(image_files)
Form.show()
sys.exit(app.exec_())
问题出在您在 MainWindow.__init__
中创建 Slides
实例的那一行
self.slides_widget = Slides(self)
您在 image_files
变量而不是 parent
的位置传递 self
(读取为 MainWindow
的实例)。查看您的 Slides.__init__
class Slides(QtGui.QWidget):
def __init__(self, image_files, parent=None):
parent
变量具有默认值 None
,因此在创建实例期间不会触发错误。但是,稍后当您尝试将 image_files
视为列表时,您会得到一个 TypeError
(记住您已经放置了 image_files=self
(MainWindow
实例))。
只需使用:
self.slides_widget = Slides(image_files, self)
而不是在你的 MainWindow.__init__
祝你好运:)
我创建了两个 classes - 从 QtDesigner 生成的 QMainWindow 和单独执行时效果完美的 QWidget 幻灯片。我设法将小部件合并到 MainWindow class 中,它执行得很好但是当我单击幻灯片按钮时,图像没有显示并出现此错误:
line 61, in timerEvent
if self.step >= len(self.image_files):
TypeError: object of type 'MainWindow' has no len()
这可能是一个小错误,但我对 PyQt 还是个新手,我已经尝试摆弄它差不多一天了,哈哈。一如既往,我们将不胜感激!
完整代码如下:
from PyQt4 import QtCore, QtGui
import sys
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 MainWindow(QtGui.QMainWindow):
#(self, parent=None) <- original code
def __init__(self, image_files, parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.setupUi(self)
#Initialized Widget here
self.slides_widget = Slides(self)
self.setCentralWidget(self.slides_widget)
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(1012, 532)
self.tabWidget = QtGui.QTabWidget(Form)
self.tabWidget.setGeometry(QtCore.QRect(470, 130, 451, 301))
self.tabWidget.setObjectName(_fromUtf8("tabWidget"))
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "Form", None))
class Slides(QtGui.QWidget):
def __init__(self, image_files, parent=None):
QtGui.QWidget.__init__(self, parent)
self.image_files = image_files
self.label = QtGui.QLabel("", self)
self.label.setGeometry(50, 150, 450, 350)
#button
self.button = QtGui.QPushButton(". . .", self)
self.button.setGeometry(200, 100, 140, 30)
self.button.clicked.connect(self.timerEvent)
self.timer = QtCore.QBasicTimer()
self.step = 0
self.delay = 3000 #ms
sTitle = "DIT Erasmus Page : {} seconds"
self.setWindowTitle(sTitle.format(self.delay/1000.0))
def timerEvent(self, e=None):
if self.step >= len(self.image_files):
self.timer.start(self.delay, self)
self.step = 0
return
self.timer.start(self.delay, self)
file = self.image_files[self.step]
image = QPixmap(file)
self.label.setPixmap(image)
self.setWindowTitle("{} --> {}".format(str(self.step), file))
self.step += 1
image_files = ["slide1.jpg", "slide2.jpg", "slide3.jpg", "slide4.jpg"]
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
Form = MainWindow(image_files)
ui = MainWindow(image_files)
Form.show()
sys.exit(app.exec_())
问题出在您在 MainWindow.__init__
Slides
实例的那一行
self.slides_widget = Slides(self)
您在 image_files
变量而不是 parent
的位置传递 self
(读取为 MainWindow
的实例)。查看您的 Slides.__init__
class Slides(QtGui.QWidget):
def __init__(self, image_files, parent=None):
parent
变量具有默认值 None
,因此在创建实例期间不会触发错误。但是,稍后当您尝试将 image_files
视为列表时,您会得到一个 TypeError
(记住您已经放置了 image_files=self
(MainWindow
实例))。
只需使用:
self.slides_widget = Slides(image_files, self)
而不是在你的 MainWindow.__init__
祝你好运:)