2017-04-21 119 views
1

我創建了兩個類 - QMainWindow,它是從QtDesigner生成的,而QWidget幻燈片在單獨執行時完美運行。我設法合併主窗口類中的小部件,它執行罰款,但是當我點擊幻燈片按鈕,圖像顯示不出來,讓這個錯誤:PyQt:QWidget幻燈片圖像在QMainWindow中沒有正確顯示

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

回答

3

問題出在哪裏,你在你的MainWindow.__init__

self.slides_widget = Slides(self) 

您通過self創建Slides實例行(讀作的MainWindow實例)上image_files變量而不是parent的地方。請參閱您的Slides.__init__

class Slides(QtGui.QWidget): 
    def __init__(self, image_files, parent=None): 

parent變量有一個默認值None這樣的實例創建過程中沒有錯誤被觸發。但是,如果您嘗試將image_files作爲列表處理(請記住您已將image_files=selfMainWindow實例)),稍後您會得到TypeError

只需使用:

self.slides_widget = Slides(image_files, self) 

而是在你MainWindow.__init__

祝你好運:)

+1

這奏效了我!許多感謝! – madLad