change url after 5 seconds

I would like to redirect to other links once the PyQt5 window and webEngine has already started. This is what I already tried:

import sys
import time
from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets

def mainPyQt5():

    url = 'https://accounts.google.com/signin/v2'
    app = QtWidgets.QApplication(sys.argv)
    browser = QtWebEngineWidgets.QWebEngineView()
    browser.load(QtCore.QUrl(url))
    browser.show()
    time.sleep(5)
    browser.load(QtCore.QUrl("https://youtube.com"))
    browser.show()
    time.sleep(5)
    sys.exit(app.exec_())

mainPyQt5()

This will just wait 5 seconds for the second link to show, it will never show the google sign in page. How can I redirect to another link after the sign in page has already loaded?

Upvotes: 1

Views: 527

Answers (1)

eyllanesc
eyllanesc

Reputation: 244202

Never use time.sleep() in a GUI because it blocks the GUI, preventing it from performing its task normally, such as displaying the window, updating the status of the GUI, etc. Instead use a QTimer.

import sys
from functools import partial
from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets

def mainPyQt5():
    url = 'https://accounts.google.com/signin/v2'
    app = QtWidgets.QApplication(sys.argv)
    browser = QtWebEngineWidgets.QWebEngineView()
    browser.load(QtCore.QUrl(url))
    browser.show()
    QtCore.QTimer.singleShot(5*1000, partial(browser.load, QtCore.QUrl("https://youtube.com")))
    sys.exit(app.exec_())
mainPyQt5()

Upvotes: 1

Related Questions