python - I cannot display a .html page on an app from PyQt6 - Stack Overflow

admin2025-04-19  0

Operating System is Windows 10. Whenever I try to display any form of .html file, whether it is local or on the internet, I either get a UI from PyQt6 that is blank, or error:

I have successfully installed required dependencies, pip install PyQt6 PyQt6-WebEngine.

Full output is

[Running] python -u "c:\Users\PC\OneDrive\Desktop\main.py"

[Done] exited with code=3221226505 in 0.46 seconds

This is my code

from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout
from PyQt6.QtWebEngineWidgets import QWebEngineView
from PyQt6.QtCore import QUrl

app = QApplication([])
window = QWidget()
layout = QVBoxLayout()

view = QWebEngineView()
layout.addWidget(view)

view.setUrl(QUrl("/"))

window.setLayout(layout)
window.show()

app.exec()

Operating System is Windows 10. Whenever I try to display any form of .html file, whether it is local or on the internet, I either get a UI from PyQt6 that is blank, or error:

I have successfully installed required dependencies, pip install PyQt6 PyQt6-WebEngine.

Full output is

[Running] python -u "c:\Users\PC\OneDrive\Desktop\main.py"

[Done] exited with code=3221226505 in 0.46 seconds

This is my code

from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout
from PyQt6.QtWebEngineWidgets import QWebEngineView
from PyQt6.QtCore import QUrl

app = QApplication([])
window = QWidget()
layout = QVBoxLayout()

view = QWebEngineView()
layout.addWidget(view)

view.setUrl(QUrl("https://www.google/"))

window.setLayout(layout)
window.show()

app.exec()
Share Improve this question edited Mar 5 at 23:42 musicamante 48.8k8 gold badges41 silver badges74 bronze badges asked Mar 5 at 23:10 DogeDoge 212 bronze badges 5
  • We need more details. Please edit your question and add specific OS and Qt (not PyQt) versions and any output resulting from running the code above from a terminal or prompt. – musicamante Commented Mar 5 at 23:20
  • @musicamante I believe I have added what you asked for. I have not downloaded Qt. – Doge Commented Mar 5 at 23:27
  • PyQt is a Python binding around the Qt library, the fact that you didn't expressly download Qt doesn't matter, as its library files have been probably installed along with the PyQt distribution. You must be aware of this aspect: PyQt doesn't do anything on its own, it only allows using the Qt framework from Python, so you do have Qt; the fact that you weren't aware about it is irrelevant: please do search more about the libraries and modules you use. That said, just run the Python interpreter, import the QtCore library and see the output of QT_VERSION_STR. – musicamante Commented Mar 5 at 23:40
  • 1 Also please be more careful when editing, I had to fix again your code block syntax. Remember to always check the post preview before submitting any post or change, and ensure that code blocks are properly displayed. If you don't know how code formatting works, then please do proper research. See the help center and how to format code. – musicamante Commented Mar 5 at 23:43
  • did you try to run without setUrl? Maybe problem is in different place. You could use print() to see which lines were executed before error. – furas Commented Mar 6 at 0:46
Add a comment  | 

1 Answer 1

Reset to default 0

Running this I get:

Argument list is empty, the program name is not passed to QCoreApplication. base::CommandLine cannot be properly initialized.

So your program DOES print an error. But the way you run python (from inside another process?) seems to suppress / ignore the stderr.

To fix it, pass sys.argv (or some non-empty dummy list) to your QApplication():

import sys
# ...
app = QApplication(sys.argv)

From the docs for QApplication:

Warning: [...] In addition, argc must be greater than zero and argv must contain at least one valid character string.

Which translated to Python means the list passed to the constructor needs to be non-empty.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745003167a279353.html

最新回复(0)