import
sys
import
numpy as np
from
PyQt5.QtWidgets
import
QApplication, QMainWindow, QLabel, QVBoxLayout, QWidget, QHBoxLayout, QFileDialog, QMenuBar, QMenu, QAction, QMessageBox
from
PyQt5.QtGui
import
QPixmap, QImage, QKeyEvent, QMouseEvent
from
PyQt5.QtCore
import
Qt, QTimer, QPropertyAnimation, QEasingCurve
import
fitz
from
PyQt5.QtWidgets
import
QWIDGETSIZE_MAX
from
playsound
import
playsound
class
BookReader(QMainWindow):
def
__init__(
self
, book_cover):
super
().__init__()
self
.book_cover
=
book_cover
self
.book_pdf
=
None
self
.current_page
=
0
self
.total_pages
=
0
self
.animation
=
None
self
.pdf_loaded
=
False
self
.initUI()
def
mousePressEvent(
self
, event: QMouseEvent):
if
event.button()
=
=
Qt.LeftButton:
self
.flipPage(
1
)
elif
event.button()
=
=
Qt.RightButton:
self
.flipPage(
-
1
)
super
().mousePressEvent(event)
def
keyPressEvent(
self
, event: QKeyEvent):
if
event.key()
=
=
Qt.Key_Up:
self
.flipPage(
-
1
)
elif
event.key()
=
=
Qt.Key_Down:
self
.flipPage(
1
)
super
().keyPressEvent(event)
def
flipPage(
self
, direction):
next_page
=
self
.current_page
+
direction
if
next_page <
0
:
return
if
next_page >
=
self
.total_pages:
return
self
.current_page
=
next_page
try
:
playsound(
'fy.mp3'
)
except
Exception as e:
print
(f
"播放音效时出错: {e}"
)
self
.showPage(
self
.current_page)
def
initUI(
self
):
self
.setStyleSheet(
)
self
.setWindowTitle(
"PDF文件阅读器"
)
self
.setGeometry(
100
,
100
,
1280
,
920
)
self
.setMinimumSize(
800
,
450
)
self
.setMaximumSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX)
self
.central_widget
=
QWidget()
self
.setCentralWidget(
self
.central_widget)
self
.layout
=
QVBoxLayout()
self
.central_widget.setLayout(
self
.layout)
menubar
=
self
.menuBar()
file_menu
=
menubar.addMenu(
'文件'
)
open_action
=
QAction(
'打开PDF'
,
self
)
open_action.triggered.connect(
self
.loadPDF)
file_menu.addAction(open_action)
about_action
=
QAction(
'关于软件'
,
self
)
about_action.triggered.connect(
self
.about)
file_menu.addAction(about_action)
self
.book_label
=
QLabel(
self
)
self
.book_label.setAlignment(Qt.AlignCenter)
self
.book_label.resize(
self
.size())
self
.layout.addWidget(
self
.book_label)
self
.showCover()
def
showCover(
self
):
pixmap
=
QPixmap(
self
.book_cover)
scaled_pixmap
=
pixmap.scaled(
self
.book_label.size(), Qt.KeepAspectRatio)
self
.book_label.setPixmap(scaled_pixmap)
def
loadPDF(
self
):
file_dialog
=
QFileDialog()
file_dialog.setNameFilter(
"PDF Files (*.pdf)"
)
if
file_dialog.exec_():
self
.book_pdf
=
file_dialog.selectedFiles()[
0
]
self
.doc
=
fitz.
open
(
self
.book_pdf)
self
.total_pages
=
len
(
self
.doc)
cover_doc
=
fitz.
open
()
cover_page
=
cover_doc.new_page()
cover_page.insert_image(cover_page.rect, filename
=
self
.book_cover)
self
.doc.insert_pdf(cover_doc, start_at
=
0
)
self
.total_pages
+
=
1
self
.current_page
=
1
self
.showPage(
self
.current_page)
self
.pdf_loaded
=
True
else
:
self
.pdf_loaded
=
False
def
showPage(
self
, page_num):
if
0
<
=
page_num <
self
.total_pages:
self
.central_widget
=
QWidget()
self
.setCentralWidget(
self
.central_widget)
self
.layout
=
QVBoxLayout()
self
.central_widget.setLayout(
self
.layout)
if
page_num
=
=
0
:
page
=
self
.doc.load_page(page_num)
scale
=
min
(
self
.width()
/
page.rect.width,
self
.height()
/
page.rect.height)
matrix
=
fitz.Matrix(scale, scale)
pix
=
page.get_pixmap(matrix
=
matrix)
img
=
QImage(pix.samples, pix.width, pix.height, pix.stride, QImage.Format_RGB888)
pixmap
=
QPixmap.fromImage(img)
hlayout
=
QHBoxLayout()
hlayout.addStretch()
label
=
QLabel(
self
)
label.setPixmap(pixmap.scaled(
self
.width(),
self
.height(), Qt.KeepAspectRatio))
hlayout.addWidget(label)
hlayout.addStretch()
self
.layout.addLayout(hlayout)
else
:
hlayout
=
QHBoxLayout()
page1
=
self
.doc.load_page(page_num)
scale
=
min
((
self
.width()
/
2
)
/
page1.rect.width,
self
.height()
/
page1.rect.height)
matrix
=
fitz.Matrix(scale, scale)
pix1
=
page1.get_pixmap(matrix
=
matrix)
img1
=
QImage(pix1.samples, pix1.width, pix1.height, pix1.stride, QImage.Format_RGB888)
pixmap1
=
QPixmap.fromImage(img1)
label1
=
QLabel(
self
)
label1.setPixmap(pixmap1.scaled(
self
.width()
/
/
2
,
self
.height(), Qt.KeepAspectRatio))
hlayout.addWidget(label1)
if
page_num
+
1
<
self
.total_pages:
page2
=
self
.doc.load_page(page_num
+
1
)
pix2
=
page2.get_pixmap(matrix
=
matrix)
img2
=
QImage(pix2.samples, pix2.width, pix2.height, pix2.stride, QImage.Format_RGB888)
pixmap2
=
QPixmap.fromImage(img2)
label2
=
QLabel(
self
)
label2.setPixmap(pixmap2.scaled(
self
.width()
/
/
2
,
self
.height(), Qt.KeepAspectRatio))
hlayout.addWidget(label2)
self
.central_widget.layout().addLayout(hlayout)
else
:
self
.showCover()
def
resizeEvent(
self
, event):
self
.showPage(
self
.current_page)
def
completeFlip(
self
, page_num):
self
.current_page
=
page_num
self
.showPage(
self
.current_page)
self
.animation
=
None
def
about(
self
):
QMessageBox.about(
self
,
"关于"
,
"这是AI写的PDF文件阅读器。"
)
if
__name__
=
=
"__main__"
:
app
=
QApplication(sys.argv)
reader
=
BookReader(
"book.jpg"
)
reader.show()
sys.exit(app.exec_())