使用 pyqt webkit 将 Django 应用程序转换为桌面应用程序

Converting a django app to desktop app using pyqt webkit

我使用 django 创建了一个网络应用程序。但是,我需要一个相同的桌面应用程序,所以我使用了 PyQt 的 webkit。除了文件下载,整个事情都很好。在我的网络应用程序中,服务器在多个位置提供可下载文件。但是,在桌面版应用程序中单击本应触发下载的按钮时,没有任何反应。

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
app = QApplication(sys.argv)
web = QWebView()
web.load(QUrl("http://localhost:8000/home"))
#web.page().setForwardUnsupportedContent(True)
#web.page().unsupportedContent.connect(save_file_callback)
web.show()
sys.exit(app.exec_())
app = QApplication(sys.argv)
web = QWebView(loadFinished=load_finished)
web.load(QUrl("http://localhost:8000/home"))
web.page().setForwardUnsupportedContent(True)
web.page().unsupportedContent.connect(save_file_callback)
web.show()
sys.exit(app.exec_())

def save_file_callback(reply):
    try:
        with urllib.request.urlopen(reply.url().toString()) as response:
            with open('downloaded_file.ext', 'wb') as f:
                f.write(response.read())
            except Exception as e:
                QMessageBox.critical(None, 'Saving Failed!', str(e), QMessageBox.Ok)

def load_finished(reply):
    if not reply:
        QMessageBox.critical(None, 'Error!', 'An error occurred trying to load the page.', QMessageBox.Ok)

我还没有测试代码,但这应该能让您走上正确的道路。该代码是从我最近的项目中借用的,该项目在 webview 中将 Django 项目作为桌面应用程序运行 - https://github.com/awecode/django-runner