CherryPy是网页服务器,PySide是Qt的Python绑定,Django是网页开发框架。使用Django开发网页应用,之后用CherryPy作为本地的网页服务器,最后用Qt的WebKit来渲染网页。通过这一流程,我们可以使用Django框架来开发桌面应用。
为了分发程序,我们可以使用PyInstaller将应用程序打包为独立的可执行文件。PyInstaller 在2.1的版本中添加了对Django应用的支持。
以下代码示例了如何设置并启动WebServer,之后启动一个WebKit浏览器渲染显示相关内容。
import os
import sys
os.environ[ 'DJANGO_SETTINGS_MODULE' ] = ‘settings’
sys.path.append( ‘mysite’)
from mysite import settings
import cherrypy
from django.core.handlers.wsgi import WSGIHandler
from django.contrib.staticfiles.handlers import StaticFilesHandler
from PySide.QtCore import QUrl
from PySide.QtGui import QApplication, QMessageBox
from PySide.QtWebKit import QWebView
if __name__ == ‘__main__’:
print ‘mysite is running … …’
# Set up site-wide config first so we get a log if errors occur.
cherrypy.config.update({
‘environment’: ‘production’,
‘log.error_file’: ‘site.log’,
‘log.screen’: True,
})
# run CherryPy
full_media_path = os.path.dirname( os.path.abspath( sys.argv[ 0 ])) +\
settings.ADMIN_MEDIA_PREFIX
cherrypy.tree.graft( StaticFilesHandler( WSGIHandler()))
cherrypy.server.socket_port = settings.WEB_SERVER_PORT
try:
cherrypy.server.start()
except IOError:
app = QApplication(sys.argv)
msgBox = QMessageBox()
msgBox.setWindowTitle(‘mysite error’)
msgBox.setText(‘mysite is already running, please check.’)
msgBox.show()
app.exec_()
sys.exit()
# except paragraph ended here
###########################
app = QApplication(sys.argv)
web = QWebView()
web.load(QUrl(“http://127.0.0.1:WEB_SERVER_PORT/”))
web.setWindowTitle(‘mysite’)
web.show()
app.exec_()
cherrypy.server.stop()
sys.exit()