2016-07-22 81 views
0

我想在Python中做一個簡單的幻燈片來查看0.html,1.html和2.html與他們之間3秒的延遲。幻燈片的網頁(python)

下面的腳本顯示0.html在3秒內,然後我得到一個「分段錯誤(核心轉儲)」的錯誤。有任何想法嗎?

我迄今爲止代碼:

#!/usr/bin/python 
import sys 

from PyQt4.QtCore import * 
from PyQt4.QtGui import * 
from PyQt4.QtWebKit import * 
import urllib 


class Window(QWidget): 

    def __init__(self, url, dur): 
     super(Window, self).__init__() 
     view = QWebView(self) 
     layout = QVBoxLayout(self) 
     layout.setContentsMargins(0, 0, 0, 0) 
     layout.addWidget(view) 

     html = urllib.urlopen(url).read()  
     view.setHtml(html) 
     QTimer.singleShot(dur * 1000, self.close) 


def playWidget(url, dur): 
     app = QApplication(sys.argv) 
     window = Window(url, dur) 
     window.showFullScreen() 
     app.exec_() 

x = 0 
while (x < 3): 
    page = "%s.html" % x 
    playWidget(page , 3) 
    x = x + 1 

回答

0

不能創建多個QApplication,這就是爲什麼你的榜樣轉儲核心。但無論如何,如果您的程序需要創建一個全新的瀏覽器窗口來顯示每個頁面,那麼這是一個相當糟糕的設計。你應該做的是將每個新頁面加載到同一個瀏覽器中。

這是你的腳本重新寫,做的是:

#!/usr/bin/python 
import sys 

from PyQt4.QtCore import * 
from PyQt4.QtGui import * 
from PyQt4.QtWebKit import * 
import urllib 


class Window(QWidget): 
    def __init__(self, urls, dur): 
     super(Window, self).__init__() 
     self.urls = urls 
     self.duration = dur * 1000 
     self.view = QWebView(self) 
     layout = QVBoxLayout(self) 
     layout.setContentsMargins(0, 0, 0, 0) 
     layout.addWidget(self.view) 
     self.nextUrl() 

    def nextUrl(self): 
     if self.urls: 
      url = self.urls.pop(0) 
      html = urllib.urlopen(url).read() 
      self.view.setHtml(html) 
      QTimer.singleShot(self.duration, self.nextUrl) 
     else: 
      self.close() 

def playWidget(urls, dur): 
    app = QApplication(sys.argv) 
    window = Window(urls, dur) 
    window.showFullScreen() 
    app.exec_() 

urls = [ 
    'https://tools.ietf.org/html/rfc20', 
    'https://tools.ietf.org/html/rfc768', 
    'https://tools.ietf.org/html/rfc791', 
    ] 

playWidget(urls, 3)