使用HTML/CSS/JS开发桌面程序(基于QWebkit),cssqwebkit,server.pyfro


server.py

from PySide.QtCore import *from PySide.QtGui import *from PySide.QtWebKit import *import jsonclass service(QObject):    @Slot(str, str, result=str)    def fetch(self, path, data):        path = path.replace('/', '.')        lastdotidx = path.rindex('.')        modulepath = path[:lastdotidx]        funcname = path[lastdotidx+1:]        params = json.loads(data)        targetmodule =  __import__(modulepath)        targetfunc = getattr(targetmodule, funcname)        if isinstance(params, dict):            return json.dumps(targetfunc(**params))@Slot()def regist_service():    frame.addToJavaScriptWindowObject('__service__', service())@Slot(str)def on_title_changed(title):    web.setWindowTitle(title)app = QApplication('wk')web = QWebView()web.settings().setAttribute(QWebSettings.DeveloperExtrasEnabled, True)frame = web.page().mainFrame()frame.javaScriptWindowObjectCleared.connect(regist_service)web.setUrl(QUrl('index.html'))web.titleChanged.connect(on_title_changed)web.show()app.exec_()

test.py

def testfunc(name):    return "Hello :%s<br />" % name

index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html><head><script>    for(var i=0; i<10; i++)        document.write(__service__.fetch('test/testfunc', '{"name":"asdf"}'))</script></head><body></body></html>

评论关闭