gpt4 book ai didi

Python Shell 在使用 PyQt5 的 while 循环后重新启动

转载 作者:行者123 更新时间:2023-12-01 07:33:59 25 4
gpt4 key购买 nike

实际上我尝试做一个html页面的显示。我有一些需要显示的 html 列表,每个页面必须在 x 秒内保持可见。但是,显示第一页后,应用程序崩溃并导致 python shell 重新启动。

在我看来,我会创建窗口来显示页面并关闭应用程序,因为之后我将尝试显示 png/jpg,所以我需要关闭应用程序以使用 Pygame 处理图片并重新构建应用程序来显示之后的html页面。我的 list 寻找它:html页面/html页面/图片/html页面/图片/图片

所以我构建了一个示例代码来测试 while 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")

执行后的结果

这可能是 sys.argv in app var 的错误,但我是 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")

关于Python Shell 在使用 PyQt5 的 while 循环后重新启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57085292/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com