吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 886|回复: 24
收起左侧

[讨论] AI生成的PDF文件阅读器,已经可用,需大侠继续修改。

[复制链接]
liuyang207 发表于 2025-3-17 16:01
本帖最后由 liuyang207 于 2025-3-17 16:43 编辑

这个AI生成的PDF文件阅读器,哪位大侠继续,我想实现翻页时页面的卷曲效果,但AI搞不定。:lol
程序目录有book.jpg、fy.mp3两个文件,一个是启动时显示的图片,一个是翻页时的音效。

微信图片_20250317160631.png
[Python] 纯文本查看 复制代码
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
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  # PyMuPDF
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  # 初始化时不指定 PDF 文件
        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("""
            QMainWindow {
                background-color: #f0f0f0;
            }
            QLabel {
                background-color: white;
                border: 1px solid #ccc;
                border-radius: 5px;
            }
            QMenuBar {
                background-color: #e0e0e0;
                color: black;
            }
            QMenuBar::item {
                background-color: #e0e0e0;
                color: black;
            }
            QMenuBar::item:selected {
                background-color: #d0d0d0;
            }
            QMenu {
                background-color: #e0e0e0;
                color: black;
            }
            QMenu::item:selected {
                background-color: #d0d0d0;
            }
        """)
        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()  # 创建新的空 PDF 文档
            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)
        # 删除不必要的调用
        # super().resizeEvent(event)
 
    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_())






微信图片_20250317160634.png
book.jpg

免费评分

参与人数 4吾爱币 +4 热心值 +2 收起 理由
猫吃 + 1 我很赞同!
pyjiujiu + 1 + 1 用心讨论,共获提升!
Robinbnbnbm + 1 鼓励转贴优秀软件安全工具和文档!
dhwl9899 + 1 + 1 谢谢@Thanks!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

netqianxi 发表于 2025-3-17 17:36
谢谢分享。。。。。
dafen444 发表于 2025-3-17 18:00
我用AI优化的,通过这些修改,你可以在翻页时看到页面的卷曲效果。希望这对你有帮助!
要在翻页时添加卷曲效果,可以使用 `QPropertyAnimation` 来实现页面的动画效果。我们可以通过在 `flipPage` 方法中添加动画来实现这一点。以下是修改后的代码:

```python
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, QRect
import fitz  # PyMuPDF
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  # 初始化时不指定 PDF 文件
        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 or next_page >= self.total_pages:
            return

        # 播放翻页音效
        try:
            playsound('fy.mp3')
        except Exception as e:
            print(f"播放音效时出错: {e}")

        if direction == 1:
            self.animateFlip(next_page, True)
        else:
            self.animateFlip(next_page, False)

    def animateFlip(self, next_page, is_forward):
        if self.animation:
            self.animation.stop()

        self.next_page = next_page
        self.is_forward = is_forward

        if is_forward:
            start_rect = QRect(0, 0, self.width() // 2, self.height())
            end_rect = QRect(self.width() // 2, 0, self.width() // 2, self.height())
        else:
            start_rect = QRect(self.width() // 2, 0, self.width() // 2, self.height())
            end_rect = QRect(0, 0, self.width() // 2, self.height())

        self.animation = QPropertyAnimation(self, b"geometry")
        self.animation.setDuration(500)
        self.animation.setStartValue(start_rect)
        self.animation.setEndValue(end_rect)
        self.animation.setEasingCurve(QEasingCurve.InOutQuad)
        self.animation.finished.connect(self.completeFlip)
        self.animation.start()

    def completeFlip(self):
        self.current_page = self.next_page
        self.showPage(self.current_page)
        self.animation = None

    def initUI(self):
        # 设置窗口样式
        self.setStyleSheet("""
            QMainWindow {
                background-color: #f0f0f0;
            }
            QLabel {
                background-color: white;
                border: 1px solid #ccc;
                border-radius: 5px;
            }
            QMenuBar {
                background-color: #e0e0e0;
                color: black;
            }
            QMenuBar::item {
                background-color: #e0e0e0;
                color: black;
            }
            QMenuBar::item:selected {
                background-color: #d0d0d0;
            }
            QMenu {
                background-color: #e0e0e0;
                color: black;
            }
            QMenu::item:selected {
                background-color: #d0d0d0;
            }
        """)
        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()  # 创建新的空 PDF 文档
            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)
        # 删除不必要的调用
        # super().resizeEvent(event)

    def completeFlip(self):
        self.current_page = self.next_page
        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_())
```

### 解释
1. **`animateFlip` 方法**:这个方法负责创建翻页动画。根据翻页方向(向前或向后),设置动画的起始和结束矩形。
2. **`completeFlip` 方法**:动画完成后,更新当前页面并显示新的页面。
3. **`flipPage` 方法**:在翻页时调用 `animateFlip` 方法,启动动画。
uko233 发表于 2025-3-17 18:11
用的Claude还是grok还是trae啊,楼主有编程基础吗,容易做吗这个
sdieedu 发表于 2025-3-17 18:32
dafen444 发表于 2025-3-17 18:00
我用AI优化的,通过这些修改,你可以在翻页时看到页面的卷曲效果。希望这对你有帮助!
要在翻页时添加卷曲 ...

太强大了
wangluoyijia 发表于 2025-3-17 18:57
怎么操作让AI生成软件的
weiguanshijiao 发表于 2025-3-17 19:15
厉害了666
dhwl9899 发表于 2025-3-17 19:33
横空出世,这个是值得关注的。谢谢分享。
zhuhao0117 发表于 2025-3-17 19:36
AI编程是不是很方便
takajihan 发表于 2025-3-17 20:30
基础程序员感觉要饿肚子了
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-4-14 02:40

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表