Advertisement
danfalck

simple js python qtwebkit connection

Apr 19th, 2012
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.26 KB | None | 0 0
  1. import sys
  2. from PyQt4 import QtCore, QtGui, QtWebKit
  3.  
  4. """Html snippet."""
  5. html = """
  6. <html><body>
  7.  <center>
  8.  <script language="JavaScript">
  9.    document.write('<p>Python ' + pyObj.pyVersion + '</p>')
  10.  </script>
  11.  <button onClick="pyObj.showMessage('Hello from WebKit')">Press me</button>
  12.  </center>
  13. </body></html>
  14. """
  15.  
  16. class StupidClass(QtCore.QObject):
  17.     """Simple class with one slot and one read-only property."""
  18.  
  19.     @QtCore.pyqtSlot(str)
  20.     def showMessage(self, msg):
  21.         """Open a message box and display the specified message."""
  22.         QtGui.QMessageBox.information(None, "Info", msg)
  23.  
  24.     def _pyVersion(self):
  25.         """Return the Python version."""
  26.         return sys.version
  27.  
  28.     """Python interpreter version property."""
  29.     pyVersion = QtCore.pyqtProperty(str, fget=_pyVersion)
  30.  
  31. def main():
  32.     app = QtGui.QApplication(sys.argv)
  33.  
  34.     myObj = StupidClass()
  35.  
  36.     webView = QtWebKit.QWebView()
  37.     # Make myObj exposed as JavaScript object named 'pyObj'
  38.     webView.page().mainFrame().addToJavaScriptWindowObject("pyObj", myObj)
  39.     webView.setHtml(html)
  40.  
  41.     window = QtGui.QMainWindow()
  42.     window.setCentralWidget(webView)
  43.     window.show()
  44.  
  45.     sys.exit(app.exec_())
  46.  
  47. if __name__ == "__main__":
  48.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement