0

I am building an app in Python using PyQt5 and I am going to freeze it. Therefor I would like to have my python code independent from any local file path. At some point I am loading an html code with:

self.pageXXX.html_code.load(QtCore.QUrl.fromLocalFile(QtCore.QDir.current().filePath("example.html")))

How can I change this command so that I can paste the HTML code directly in the html.load()?

Something like:

    self.pageXXX.html_code.load('''
                                      HTML code
                                ''')
4
  • Try setHtml() instead of load() Commented Nov 20, 2017 at 21:44
  • I did, but, since I have a JS code in the HTML and also qwebchannel.js, when I use setHtml() the HTML page doesn't load correctly and I get an error message: js: Uncaught ReferenceError: QWebChannel is not defined Commented Nov 20, 2017 at 21:48
  • 1
    Did u add QWebChannel correctly ? SO Commented Nov 20, 2017 at 21:52
  • I didn't! Thanks, it works! Commented Nov 20, 2017 at 22:01

1 Answer 1

1

As suggested by @Maurice Meyer, the problem was in the way I set QWebChannel. I just added

<script src="qrc:///qtwebchannel/qwebchannel.js"></script>

at the top of my HTML code and then:

from PyQt5.QtCore import QObject, pyqtSlot
from PyQt5.QtWebChannel import QWebChannel
from PyQt5.QtWebEngineWidgets import QWebEngineView

in my Python code. And the magic happened!

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.