0

I created a web browser by PyQt5 ,if I load url="http://www.google.com" have nothing issues,but if I load url = "http://192.168.0.106/get.html" ,run the code, the widgets crash.

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import *
import sys  

class MainWindow(QMainWindow):
    """docstring for MainWindow"""
    def __init__(self, *arg,**kwargs):
        super(MainWindow, self).__init__(*arg,**kwargs)
        self.setWindowTitle("Load huobi exchange bar")

        self.browser = QWebEngineView()
        self.browser.setUrl(QUrl("http://192.168.0.106/get.html"))

        self.setCentralWidget(self.browser)


app = QApplication(sys.argv)

window = MainWindow()
window.show()

app.exec_()

bellow is the content of get.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>huobi exchange bar</title>
</head>
<body>
  <!-- TradingView Widget BEGIN -->
  <div class="tradingview-widget-container">
    <div class="tradingview-widget-container__widget"></div>
    <div class="tradingview-widget-copyright"><a href="https://cn.tradingview.com/crypto-screener/" rel="noopener" target="_blank"><span class="blue-text">sample</span></a>TradingView</div>
    <script type="text/javascript" src="https://s3.tradingview.com/external-embedding/embed-widget-screener.js" async>
    {
    "width": 1100,
    "height": 512,
    "defaultColumn": "overview",
    "defaultScreen": "general",
    "market": "crypto",
    "showToolbar": true,
    "colorTheme": "dark",
    "locale": "zh_CN"
  }
    </script>
  </div>
  <!-- TradingView Widget END -->
</body>
</html>

my question is :how to solve the window crash when load about async js?

5
  • Do you have a server that loads the HTML? Commented Dec 21, 2019 at 18:23
  • yes,I do.I have a server in local host. I can open directly the get.html in chrome . Commented Dec 22, 2019 at 5:38
  • Then you must indicate how you run your host, you must give more information .... Commented Dec 22, 2019 at 5:44
  • And as I indicated in my answer: it is recommended that you run your code in the CMD/console since you will be able to obtain more information about the error. Commented Dec 22, 2019 at 5:45
  • I mean that if the content of html don't include the javascript like this :"src="s3.tradingview.com/external-embedding/embed-widget-screener.js" async " ,the widget display normally . Commented Dec 22, 2019 at 5:52

1 Answer 1

1

I do not understand why the application is broken because even if the url did not exist this should show you the error page so if you want more details of the error you should run the code in the console/CMD.

On the other hand you do not indicate that any server executes the HTML, in addition it is not necessary to use the "http://192.168.0.106" host, just load it as a local file:

├── get.html
└── main.py
import os
import sys

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


class MainWindow(QMainWindow):
    """docstring for MainWindow"""

    def __init__(self, *arg, **kwargs):
        super(MainWindow, self).__init__(*arg, **kwargs)
        self.setWindowTitle("Load huobi exchange bar")

        self.browser = QWebEngineView()

        current_dir = os.path.dirname(os.path.realpath(__file__))
        filename = os.path.join(current_dir, "get.html")
        url = QUrl.fromLocalFile(filename)
        self.browser.setUrl(url)

        self.setCentralWidget(self.browser)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

enter image description here

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

9 Comments

thanks for your help. I copy your code in my pc,excute it ,but the widget only display about 8 seconds and then auto closed .
@Baren Where do you run it? In the console/CMD or in some IDE? what is your OS?
@Baren Please just answer me in a thread, what version of PyQt5 do you use? In addition to the previous questions.
I run it in both IDE(sublime) and CMD,my OS is win10
@Baren Do you get an error message in the case of CMD? What version of python and pyqt5 do you use?
|

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.