简体   繁体   中英

Pyqt5 pdf viewer doesn't display zoom buttons or zoom percentage

I'm learning how to use pyqt, I'm using Windows 10 and python 3.10 and i'm testing a code to open pdf files, this allows you to select the file and then opens it on a new window. It works almost correctly except that doesn't show zoom buttons, or zoom percentage.

This is the code:

import sys

from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets


def main():

    print(
        f"PyQt5 version: {QtCore.PYQT_VERSION_STR}, Qt version: {QtCore.QT_VERSION_STR}"
    )

    app = QtWidgets.QApplication(sys.argv)
    filename, _ = QtWidgets.QFileDialog.getOpenFileName(None, filter="PDF (*.pdf)")
    if not filename:
        print("please select the .pdf file")
        sys.exit(0)
    view = QtWebEngineWidgets.QWebEngineView()
    settings = view.settings()
    settings.setAttribute(QtWebEngineWidgets.QWebEngineSettings.PluginsEnabled, True)
    url = QtCore.QUrl.fromLocalFile(filename)
    view.load(url)
    view.resize(640, 480)
    view.show()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()

I was wondering if I need to pass an extra setting to display zoom buttons on pdf viewer. Anyone can help me? I highlighted where buttons/actions supposed to display in the image below.

我突出显示了按钮/操作应该显示的位置

That problem is because Qt version, if you test with the most recent version, PyQt6 you will see that works.

You need to install PyQt6:

pip install pyqt6

You also need to install webengine for pyqt6:

pip install PyQt6-WebEngine

And you need to change your code to this:

import sys

from PyQt6 import QtCore, QtWidgets, QtWebEngineWidgets


def main():

    print(
        f"PyQt6 version: {QtCore.PYQT_VERSION_STR}, Qt version: {QtCore.QT_VERSION_STR}"
    )

    app = QtWidgets.QApplication(sys.argv)
    filename, _ = QtWidgets.QFileDialog.getOpenFileName(None, filter="PDF (*.pdf)")
    if not filename:
        print("please select the .pdf file")
        sys.exit(0)
    view = QtWebEngineWidgets.QWebEngineView()
    settings = view.settings()
    settings.setAttribute(view.settings().WebAttribute.PluginsEnabled, True)
    url = QtCore.QUrl.fromLocalFile(filename)
    view.load(url)
    view.resize(640, 480)
    view.show()
    sys.exit(app.exec())


if __name__ == "__main__":
    main()

And now should look like this:

加载pdf

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM