python - QTextBrowser 在 Mac 上显示 markdown,但在 Windows 上不显示

标签 python pyqt pyside pyside6 qtextbrowser

我正在使用 Qt (pyside6) 构建一个应用程序,并使用 Pyinstaller 对其进行打包。通过 pycharm 运行或打包时,一切都在 Mac 上运行得很好,但是 QTextBrowser 在我的 Windows 10 计算机上运行时不会呈现源 Markdown 文件。

我添加了一个 github 存储库的链接,该存储库是我整理的,这是我的应用程序的一个极其精简的版本。它只是一个仅显示 QTextBrowser 的 Qt 界面,它显示位于数据文件夹中的 markdown 文件。这重现了与我的实际应用程序相同的问题。在 Windows 计算机上执行应用程序时,我缺少什么来显示 Markdown 文件?

我确实验证了文件路径是否正确(我认为),当我从 self.education_textbox.sourceType() 获取源类型时,它成功打印出 markdown 资源。这让我相信这不是严格的路径问题,因为如果没有获取 markdown 文件,它会显示 UnknownResource。

https://github.com/BigMoonTech/problem_reproduction.git

这是app.py:

import sys

from PySide6.QtWidgets import QMainWindow, QApplication

from MainWindow import UiMainWindow
from infrastructure.PathResolver import resolve_path


class MainWindow(QMainWindow, UiMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.setup_ui(self)
        self.education_textbox.setSource(resolve_path('data/edu/FecalPositiveRoundworm.md'))


if __name__ == "__main__":
    app = QApplication(sys.argv)
    app.setApplicationDisplayName('MyApp')
    main_window = MainWindow()
    main_window.setWindowTitle('MyApp')
    main_window.show()
    sys.exit(app.exec())

这是 MainWindow.py:

from PySide6.QtCore import QSize
from PySide6.QtWidgets import QWidget, QVBoxLayout, QStackedWidget, QGridLayout, QTextBrowser


class UiMainWindow(object):
    def setup_ui(self, MainWindow):
        if not MainWindow.objectName():
            MainWindow.setObjectName(u"MainWindow")
        MainWindow.resize(720, 515)
        MainWindow.setMinimumSize(QSize(720, 515))

        self.centralwidget = QWidget(MainWindow)
        self.centralwidget.setObjectName(u"centralwidget")

        self.verticalLayout = QVBoxLayout(self.centralwidget)
        self.verticalLayout.setObjectName(u"verticalLayout")

        self.stackedWidget = QStackedWidget(self.centralwidget)
        self.stackedWidget.setObjectName(u"stackedWidget")

        self.page = QWidget()
        self.page.setObjectName(u"page")
        self.verticalLayout2 = QVBoxLayout(self.page)
        self.verticalLayout2.setObjectName(u"verticalLayout2")
        self.education_textbox = QTextBrowser(self.page)
        self.education_textbox.setObjectName(u"education_textbox")
        self.verticalLayout2.addWidget(self.education_textbox)


        self.education_textbox.setOpenLinks(False)

        self.stackedWidget.addWidget(self.page)
        self.verticalLayout.addWidget(self.stackedWidget)

        MainWindow.setCentralWidget(self.centralwidget)

        self.stackedWidget.setCurrentIndex(0)

这是解析文件路径的实用程序脚本,PathResolver.py:

import os
import sys


def resolve_path(path: str) -> str:
    if getattr(sys, "frozen", False):
        # If the 'frozen' flag is set, we are in bundled-app mode!
        resolved_path = os.path.abspath(os.path.join(sys._MEIPASS, path))
    else:
        # Normal development mode. Use os.getcwd() or __file__ as appropriate in your case...
        resolved_path = os.path.abspath(os.path.join(os.getcwd(), path))

    return resolved_path

最佳答案

.setSource()方法接受 QUrl 作为其第一个参数。

void QTextBrowser::setSource(const QUrl &url, QTextDocument::ResourceType type = QTextDocument::UnknownResource) Attempts to load the document at the given url with the specified type.

当它接受字符串时,它被解释为 URL。这在非 Windows 平台上可能没问题,因为路径分隔符是正确的。

要使其跨平台工作,您应该首先从本地文件路径创建一个 QUrl 对象,并将其传递给 setSource,即

from PySide6.QtCore import QUrl

url = QUrl.fromLocalFile(path)
self.education_textbox.setSource(url)

添加此步骤后,文件将正确加载。

Markdown file loading in the GUI window

关于python - QTextBrowser 在 Mac 上显示 markdown,但在 Windows 上不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73117636/

相关文章:

Python I/O : How to use the wsgi. 来自 io 模块的输入流

python 将 IplImage 转换为 Qimage

python - 在 PySide 中减小图像的大小

python - 类型错误 : could not convert

python - 如何提取内容,selectedItems() 的结果

python - 在 PyYaml 中转储为 utf-8

java - 通过蓝牙将字节从 Raspberry Pi(使用 python)发送到 java android 应用程序

Python:捕获 QListWidget 中的重新排序事件?

python - PyQT - 列出 QWidgets/Windows

python - 如何从pandas数据框中的列值中删除连续的四位数字