python 实现 pdf阅读器_ico

去年写过一篇 python 利用 PySide2&PyQt5实现 PDF 阅读器,当时讲了思路,并没有放上源代码,最近新关注的同学想要源代码,这里我就统一放上,感兴趣的可以自己取。


顺便重新录了一个视频:



ui_file_manage.py

# -*- coding: utf-8 -*-


################################################################################
## Form generated from reading UI file 'file_manage.ui'
##
## Created by: Qt User Interface Compiler version 5.15.2
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################


from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *




class Ui_FileManager(object):
def setupUi(self, FileManager):
if not FileManager.objectName():
FileManager.setObjectName(u"FileManager")
FileManager.resize(950, 719)
icon = QIcon()
icon.addFile(u"\u8bfe\u4ef6.png", QSize(), QIcon.Normal, QIcon.Off)
FileManager.setWindowIcon(icon)
self.centralwidget = QWidget(FileManager)
self.centralwidget.setObjectName(u"centralwidget")
self.tree = QTreeWidget(self.centralwidget)
__qtreewidgetitem = QTreeWidgetItem()
__qtreewidgetitem.setText(0, u"1");
self.tree.setHeaderItem(__qtreewidgetitem)
self.tree.setObjectName(u"tree")
self.tree.setGeometry(QRect(0, 0, 256, 671))
self.verticalLayoutWidget = QWidget(self.centralwidget)
self.verticalLayoutWidget.setObjectName(u"verticalLayoutWidget")
self.verticalLayoutWidget.setGeometry(QRect(260, 0, 681, 671))
self.verticalLayout = QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setObjectName(u"verticalLayout")
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
FileManager.setCentralWidget(self.centralwidget)
self.menubar = QMenuBar(FileManager)
self.menubar.setObjectName(u"menubar")
self.menubar.setGeometry(QRect(0, 0, 950, 22))
FileManager.setMenuBar(self.menubar)
self.statusbar = QStatusBar(FileManager)
self.statusbar.setObjectName(u"statusbar")
FileManager.setStatusBar(self.statusbar)


self.retranslateUi(FileManager)


QMetaObject.connectSlotsByName(FileManager)
# setupUi


def retranslateUi(self, FileManager):
FileManager.setWindowTitle(QCoreApplication.translate("FileManager", u"\u6587\u4ef6\u7ba1\u7406\u5668", None))
# retranslateUi



main.py

import sys
import os
from PySide2.QtWidgets import *
from ui_file_manage import Ui_FileManager
from PySide2.QtCore import QUrl, Qt
from PySide2.QtGui import QIcon, QBrush, QColor
from PySide2.QtWebEngineWidgets import QWebEngineView


class FileManager(QMainWindow, Ui_FileManager):
def __init__(self):
super().__init__()
self.setupUi(self)
self.init_tree()
self.browser = QWebEngineView()
settings = self.browser.settings()
settings.setAttribute(settings.PluginsEnabled, True)
settings.setAttribute(settings.PdfViewerEnabled, True)
self.verticalLayout.addWidget(self.browser)
self.show()


def init_tree(self):
self.tree.setColumnCount(1)
#隐藏头部标题
self.tree.setHeaderHidden(True)
#设置根节点
root=QTreeWidgetItem(self.tree)
root.setText(0,'文件管理器')
root.setIcon(0,QIcon('课件.png'))
#设置列的宽度
self.tree.setColumnWidth(0,200)
files = os.listdir()
for file in files:
if os.path.isdir(file) and file[0].isdigit():
child=QTreeWidgetItem(root)
child.setText(0,file)
sections = os.listdir(file)
child.setIcon(0, QIcon('文件夹.png'))
for section in sections:
child_2 = QTreeWidgetItem(child)
child_2.setText(0, section)
child_2.setIcon(0, QIcon('pdf.png'))
self.tree.addTopLevelItem(root)
self.tree.clicked.connect(self.load_pdf)
self.tree.expandAll()


def load_pdf(self):
item=self.tree.currentItem().text(0)
parent = self.tree.currentItem().parent().text(0)
self.browser.load(QUrl.fromLocalFile(os.path.realpath(f"{parent}/{item}")))


if __name__ == "__main__":
app = QApplication(sys.argv)
window = FileManager()
sys.exit(app.exec_())

(全文完)


长按二维码,加关注!叶子陪你玩

python 实现 pdf阅读器_二维码_02


欢迎转载,转载请注明出处!

欢迎关注公众微信号:叶子陪你玩编程