0

I would like to modify my GUI dynamically with the evaluateJavaScript command

I noticed that the GUI is updated only when python has finished working. I created this example code:

Base GUI:

webView = QWebView()
myObj = ExampleClass(webView)
webView.page().mainFrame().addToJavaScriptWindowObject("pyObj", myObj)
webView.setHtml('''
<html>
    <body>
        <div id="content"></div>
        <button onClick="pyObj.example()">start</button>
    </body>
</html>
''')

Python code for update Html GUI:

class ExampleClass(QtCore.QObject):
    def __init__(self, webView):
        super(ExampleClass, self).__init__(webView)
        self.webView = webView

    @QtCore.pyqtSlot()
    def example(self):
        print("start")
        for i in range(0,5):
            print(i)
            self.webView.page().mainFrame().evaluateJavaScript('document.getElementById("content").innerHTML = "'+str(i)+'";')
            time.sleep(1)

Objective: after pressing the "start" button I would like to see the numbers 0 1 2 3 4 sequentially appear in my GUI, one every second. Current result: after pressing the "start" button for 5 seconds nothing happens and then the number 4 appears

michelle.70
  • 183
  • 2
  • 9
  • In a guide it is forbidden to use time.sleep since it blocks the eventloop, in the case of Qt alternatives are provided, for example the use of QTimer. – eyllanesc Oct 03 '19 at 19:41
  • if i replace time.sleep(1) with fp = urllib.request.urlopen("http://www.python.org").close() i get the same update problem – michelle.70 Oct 04 '19 at 09:28

0 Answers0