0

I'm building a browser using PyQt5. It's a rather huge code, but this is the main problem I'm facing. The code is this:

import sys
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QApplication, QMainWindow

app = QApplication(sys.argv)

web = QWebEngineView()
file = open("example.html", "r")
html = file.read()
web.setHtml(html)
file.close()
web.show()

sys.exit(app.exec_())

The problem is that the rendering is rather strange.Image of the rendering is here. The contents of the example.html file:

<!DOCTYPE html>
<head><title>JS Example</title></head>
<h1>JS example</h1>
<p><button type = 'button' onclick = "document.getElementById('tobeshown').style.display='block'">Show hidden parts of this page</button></p>
<p id = 'tobeshown' style = "display:none">
Peekaboo!
</p>
<p>
<button type = 'button' onclick="document.getElementById('tobeshown').style.display='none'">Hide it!</button>
</p>
</body>
</html>

The expected output(this is in Mozilla Firefox browser): here

Can anyone tell me why the PyQt5 rendering engine produces those symbols at the top? And what can I do to resolve it? Thank you very much.

6
  • Validate your HTML, it seems to be broken: </button><p>that should be </button></p>?! Commented Oct 25, 2019 at 9:47
  • Sorry @MauriceMeyer that still generates the same output. Thanks for pointing out though. Commented Oct 25, 2019 at 9:50
  • Opening <body>-Tag is missing as well, fix all markup errors and it is going to work as expected :) Commented Oct 25, 2019 at 9:54
  • @MauriceMeyer it seems that the issue is with python file reading and not the rendering. When I add the line print(html) It outputs <!Doctype html> and so on. Commented Oct 25, 2019 at 10:07
  • As i said the HTML is broken, so remove 'hidden' unicode characters from there or try to save the HTML as ASCII. This ain't (real) Python, PyQt5 or webkit errors. Commented Oct 25, 2019 at 10:11

1 Answer 1

1

So the problem is not WebEngine's rendering but instead Python's reading of a file. Changing the file's encoding to UTF-8 solved the problem for me.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.