这是一个很好的问题,我被卡住了一段时间!我将向您展示两种通信方式的示例:从 python 到 javascript,反之亦然,希望对您有所帮助:
import PyQt4.QtGui as gui, PyQt4.QtWebKit as web, PyQt4.QtCore as core
class MyMainWindow(gui.QMainWindow):
proccessFinished = core.pyqtSignal()
def __init__(self, parent=None):
super(MyMainWindow,self).__init__()
self.wv = web.QWebView()
self.setCentralWidget(self.wv)
#pass this main window to javascrip
self.wv.page().mainFrame().addToJavaScriptWindowObject("mw", self)
self.wv.setHtml("""
<html>
<head>
<script language="JavaScript">
function p() {
document.write('Process Finished')
}
mw.proccessFinished.connect(p)
</script>
</head>
<body>
<h1>It works</h1>
<input type=button value=click onClick=mw.doIt()></input>
</body>
</html>
""")
@core.pyqtSlot()
def doIt(self):
print('running a long process...')
print('of course it should be on a thread...')
print('and the signal should be emmited from there...')
self.proccessFinished.emit()
app = gui.QApplication([])
mw = MyMainWindow()
mw.show()
app.exec_()