使用循环和计时器自动重新加载 webView 小部件
Reload webView widget automaticly with a loop and a timer
我正在创建一个 GUI,我想在其中显示带有 qWebView 的站点并每 10 秒自动重新加载一次。
我用 qThread 试过了,没有结果。 (我是线程新手)
如果我使用普通的 while 循环,我的 window 冻结。
from PyQt5 import QtCore, QtGui, QtWidgets, QtWebEngineWidgets
class updator(QtCore.QThread):
def __init__(self, window):
QtCore.QThread.__init__(self)
def run(self):
while True:
self.sleep(2000 * 2 / 1000)
self.window.htmlreader_BTCLiveGraph.reload()
class Ui_BTC_LiveGraph(object):
def setupUi(self, BTC_LiveGraph):
BTC_LiveGraph.setObjectName("BTC_LiveGraph")
BTC_LiveGraph.resize(1600, 900)
BTC_LiveGraph.setAccessibleDescription("")
self.htmlreader_BTCLiveGraph = QtWebEngineWidgets.QWebEngineView(
BTC_LiveGraph)
self.htmlreader_BTCLiveGraph.setGeometry(
QtCore.QRect(10, 10, 1500, 800))
self.htmlreader_BTCLiveGraph.setAccessibleDescription("")
# récupération du chemin absolu du fichier
self.mypath = os.path.dirname(__file__)
self.htmlreader_BTCLiveGraph.setUrl(QtCore.QUrl(
str("file:///" + self.mypath + "/BTC_liveGraph.html")))
self.htmlreader_BTCLiveGraph.setObjectName("htmlreader_BTCLiveGraph")
self.retranslateUi(BTC_LiveGraph)
QtCore.QMetaObject.connectSlotsByName(BTC_LiveGraph)
def retranslateUi(self, BTC_LiveGraph):
_translate = QtCore.QCoreApplication.translate
BTC_LiveGraph.setWindowTitle(
_translate("BTC_LiveGraph", "BTC Live Graph"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
BTC_LiveGraph = QtWidgets.QDialog()
ui = Ui_BTC_LiveGraph()
ui.setupUi(BTC_LiveGraph)
BTC_LiveGraph.show()
update = updator(ui)
update.start()
sys.exit(app.exec_())
我希望 qwebviewWidget 每 10 秒重新加载一次并且不会冻结 window
我认为 QTimer
是解决这个问题的更好方法,这里是一个工作示例:
import os
import sys
from PyQt5.Qt import QApplication, QMainWindow
from PyQt5.QtCore import QThread, QRect, QUrl, QCoreApplication, QMetaObject, QTimer
from PyQt5.QtWebEngineWidgets import QWebEngineView
class Ui_BTC_LiveGraph(object):
def setupUi(self, widget):
widget.setObjectName("BTC_LiveGraph")
widget.resize(1600, 900)
widget.setAccessibleDescription("")
self.htmlreader_BTCLiveGraph = QWebEngineView(widget)
self.htmlreader_BTCLiveGraph.setGeometry(QRect(10, 10, 1500, 800))
self.htmlreader_BTCLiveGraph.setAccessibleDescription("")
self.htmlreader_BTCLiveGraph.setObjectName("htmlreader_BTCLiveGraph")
self.retranslateUi(widget)
QMetaObject.connectSlotsByName(widget)
def retranslateUi(self, widget):
_translate = QCoreApplication.translate
widget.setWindowTitle(_translate("BTC_LiveGraph", "BTC Live Graph"))
class MainWindow(Ui_BTC_LiveGraph, QMainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
# You can do this here, just keep the Ui class for UI stuff
self.mypath = os.path.dirname(__file__)
self.htmlreader_BTCLiveGraph.setUrl(QUrl('http://www.whosebug.com'))
self._updator = QTimer(self)
self._updator.setSingleShot(False)
self._updator.timeout.connect(self.reload)
# Reload every 4 seconds
self._updator.start(4000)
def reload(self):
self.htmlreader_BTCLiveGraph.reload()
if __name__ == "__main__":
app = QApplication(sys.argv)
BTC_LiveGraph = MainWindow()
BTC_LiveGraph.show()
sys.exit(app.exec_())
我正在创建一个 GUI,我想在其中显示带有 qWebView 的站点并每 10 秒自动重新加载一次。
我用 qThread 试过了,没有结果。 (我是线程新手) 如果我使用普通的 while 循环,我的 window 冻结。
from PyQt5 import QtCore, QtGui, QtWidgets, QtWebEngineWidgets
class updator(QtCore.QThread):
def __init__(self, window):
QtCore.QThread.__init__(self)
def run(self):
while True:
self.sleep(2000 * 2 / 1000)
self.window.htmlreader_BTCLiveGraph.reload()
class Ui_BTC_LiveGraph(object):
def setupUi(self, BTC_LiveGraph):
BTC_LiveGraph.setObjectName("BTC_LiveGraph")
BTC_LiveGraph.resize(1600, 900)
BTC_LiveGraph.setAccessibleDescription("")
self.htmlreader_BTCLiveGraph = QtWebEngineWidgets.QWebEngineView(
BTC_LiveGraph)
self.htmlreader_BTCLiveGraph.setGeometry(
QtCore.QRect(10, 10, 1500, 800))
self.htmlreader_BTCLiveGraph.setAccessibleDescription("")
# récupération du chemin absolu du fichier
self.mypath = os.path.dirname(__file__)
self.htmlreader_BTCLiveGraph.setUrl(QtCore.QUrl(
str("file:///" + self.mypath + "/BTC_liveGraph.html")))
self.htmlreader_BTCLiveGraph.setObjectName("htmlreader_BTCLiveGraph")
self.retranslateUi(BTC_LiveGraph)
QtCore.QMetaObject.connectSlotsByName(BTC_LiveGraph)
def retranslateUi(self, BTC_LiveGraph):
_translate = QtCore.QCoreApplication.translate
BTC_LiveGraph.setWindowTitle(
_translate("BTC_LiveGraph", "BTC Live Graph"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
BTC_LiveGraph = QtWidgets.QDialog()
ui = Ui_BTC_LiveGraph()
ui.setupUi(BTC_LiveGraph)
BTC_LiveGraph.show()
update = updator(ui)
update.start()
sys.exit(app.exec_())
我希望 qwebviewWidget 每 10 秒重新加载一次并且不会冻结 window
我认为 QTimer
是解决这个问题的更好方法,这里是一个工作示例:
import os
import sys
from PyQt5.Qt import QApplication, QMainWindow
from PyQt5.QtCore import QThread, QRect, QUrl, QCoreApplication, QMetaObject, QTimer
from PyQt5.QtWebEngineWidgets import QWebEngineView
class Ui_BTC_LiveGraph(object):
def setupUi(self, widget):
widget.setObjectName("BTC_LiveGraph")
widget.resize(1600, 900)
widget.setAccessibleDescription("")
self.htmlreader_BTCLiveGraph = QWebEngineView(widget)
self.htmlreader_BTCLiveGraph.setGeometry(QRect(10, 10, 1500, 800))
self.htmlreader_BTCLiveGraph.setAccessibleDescription("")
self.htmlreader_BTCLiveGraph.setObjectName("htmlreader_BTCLiveGraph")
self.retranslateUi(widget)
QMetaObject.connectSlotsByName(widget)
def retranslateUi(self, widget):
_translate = QCoreApplication.translate
widget.setWindowTitle(_translate("BTC_LiveGraph", "BTC Live Graph"))
class MainWindow(Ui_BTC_LiveGraph, QMainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
# You can do this here, just keep the Ui class for UI stuff
self.mypath = os.path.dirname(__file__)
self.htmlreader_BTCLiveGraph.setUrl(QUrl('http://www.whosebug.com'))
self._updator = QTimer(self)
self._updator.setSingleShot(False)
self._updator.timeout.connect(self.reload)
# Reload every 4 seconds
self._updator.start(4000)
def reload(self):
self.htmlreader_BTCLiveGraph.reload()
if __name__ == "__main__":
app = QApplication(sys.argv)
BTC_LiveGraph = MainWindow()
BTC_LiveGraph.show()
sys.exit(app.exec_())