Search code examples
pythonpyqt4qapplication

Show fullscreen webpage in 5 seconds then close window (python)


I want to make a python script that opens up a local html file in browser and after 5 seconds close that window.

I've tried the self.close() method and if I add "time.sleep()" it will only delay the display of the web content

Here's my code (I'm a newbie so, sorry for that)

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


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)
        time.sleep(dur)
        self.close()


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

main('test.html', 5)

Any suggestions? As you can see I want to pass both url and duration (sleep) in the constructor.


Solution

  • Use a single-shot timer (the interval is in milliseconds):

    class Window(QWidget):
        def __init__(self, url, dur):
            ...
            view.setHtml(html)
            QTimer.singleShot(dur * 1000, self.close)