[英]Python Shell restart after while loop with PyQt5
實際上我嘗試做一個html頁面的顯示。 我有一些需要顯示的 html 列表,每個頁面必須在 x 秒時保持可見。 但是,在顯示第一頁后,應用程序崩潰並導致python shell 重新啟動。
在我看來,我會創建窗口來顯示頁面並關閉應用程序,因為之后我將嘗試顯示 png/jpg,所以我需要關閉應用程序以使用 Pygame 獲取圖片並重新構建應用程序以顯示 html 頁面之后. 我正在尋找它的列表:html 頁面/html 頁面/圖片/html 頁面/圖片/圖片
所以我已經構建了一個示例代碼來測試,而 boucle 顯示器:
from PyQt5 import QtWidgets, QtWebEngineWidgets, QtCore
import sys
continuer = True
while continuer:
print("Application created")
# Create application
app = QtWidgets.QApplication(sys.argv)
# Add window
win = QtWidgets.QWidget()
win.setWindowTitle('My first rendering')
# Add layout
layout = QtWidgets.QVBoxLayout()
win.setLayout(layout)
# Create QWebView
view = QtWebEngineWidgets.QWebEngineView()
view.setUrl(QtCore.QUrl('https://google.com'))
# Add QWebView to the layout
layout.addWidget(view)
# Show window, run app
win.show()
QtCore.QTimer.singleShot(7*1000, win.close)
QtCore.QTimer.singleShot(7*1000, app.quit)
print("View displayed")
# While loop
app.exec_()
print('Close Application')
print("End While Loop")
執行后的結果
它可能是 app var 中的 sys.argv 錯誤,但我是 Python 新手,所以我不知道如何解決這個問題。
問題是 QApplication 不一定會被淘汰,因此您將創建多個 Qt 禁止的 QApplication,更好的解決方案是重新考慮它,驗證它是否不存在,然后創建一個新的:
import sys
from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets
continuer = True
while continuer:
print("Application created")
# Create application
app = QtWidgets.QApplication.instance()
if app is None:
app = QtWidgets.QApplication(sys.argv)
# Add window
win = QtWidgets.QWidget()
win.setWindowTitle("My first rendering")
# Create QWebEngineView
view = QtWebEngineWidgets.QWebEngineView()
view.setUrl(QtCore.QUrl("https://google.com"))
# Add layout
layout = QtWidgets.QVBoxLayout(win)
win.setLayout(layout)
# Add QWebView to the layout
layout.addWidget(view)
# Show window, run app
win.show()
QtCore.QTimer.singleShot(7 * 1000, win.close)
QtCore.QTimer.singleShot(7 * 1000, app.quit)
print("View displayed")
# While loop
app.exec_()
print("Close Application")
print("End While Loop")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.