[Python] 纯文本查看 复制代码 from PyQt5.QtWidgets import QApplication, QMainWindow, QTreeWidget, QTreeWidgetItem, QTextEdit, QDockWidget, QWidget, \
QHBoxLayout, QPushButton, QFileDialog
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QColor, QPalette
from PyQt5.Qt import QTextCursor
import random
import re
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.resize(800, 600)
self.setWindowTitle("小说阅读器")
self.pattern1 = "第[1234567890]*章.*"
title_win = QWidget(self)
title_layout = QHBoxLayout()
title_win.setLayout(title_layout)
read_button = QPushButton("读取文本")
big_font = QPushButton("字体变大")
small_font = QPushButton("字体变小")
background_color = QPushButton("随机背景颜色")
title_layout.addWidget(read_button)
title_layout.addWidget(big_font)
title_layout.addWidget(small_font)
title_layout.addWidget(background_color)
read_button.clicked.connect(self.read_book)
self.menu_dock = QDockWidget('菜单', self)
self.menu_dock.setWidget(title_win)
self.menu_dock.setFloating(False)
self.menu_dock.setFeatures(QDockWidget.NoDockWidgetFeatures) # 设置不可脱离
self.list_dock = QDockWidget('目录栏', self)
# 创建目录侧边栏
self.tree_widget = QTreeWidget()
self.main_text = QTextEdit()
self.font_size = 12
self.main_text.setFontPointSize(self.font_size)
self.setCentralWidget(self.main_text)
self.list_dock.setWidget(self.tree_widget)
self.list_dock.setFloating(False)
self.tree_widget.setHeaderHidden(True) # 隐藏表头
# self.list_dock.setFeatures(QDockWidget.NoDockWidgetFeatures) # 设置不可脱离
# self.list_dock.setMinimumSize(200, 100) # 宽度,高度
# self.list_dock.setMaximumSize(200, 600) # 宽度,高度
self.addDockWidget(Qt.LeftDockWidgetArea, self.list_dock)
self.addDockWidget(Qt.TopDockWidgetArea, self.menu_dock)
# 设置点击事件
big_font.clicked.connect(self.font_big)
small_font.clicked.connect(self.font_small)
background_color.clicked.connect(self.update_back)
self.tree_widget.itemClicked.connect(self.on_item_clicked)
# 随机设置背景颜色
def on_item_clicked(self, item, column):
# 获取点击的章节名称
chapter_name = item.text(column)
# 根据章节名称进行跳转操作
tc = self.main_text.textCursor()
tc.setPosition(self.chapter_index_dict[chapter_name][0], QTextCursor.MoveAnchor)
tc.setPosition(self.chapter_index_dict[chapter_name][1], QTextCursor.KeepAnchor)
self.main_text.setTextCursor(tc)
def read_book(self):
file_dialog = QFileDialog()
file_dialog.setNameFilter("Text files (*.txt)")
file_dialog.exec_()
selected_files = file_dialog.selectedFiles()
if selected_files:
file_path = selected_files[0]
with open(file_path, "r", encoding='utf-8') as f:
self.content = f.read()
# print(self.content)
res = [i.span() for i in re.finditer(self.pattern1, self.content)]
self.chapter_index_dict = {}
root_item = QTreeWidgetItem(self.tree_widget, ["目录"])
for item in res:
self.chapter_index_dict[self.content[item[0]:item[1]]] = item
tmp_chapter = QTreeWidgetItem(root_item, [self.content[item[0]:item[1]]])
self.main_text.setText(self.content)
def font_big(self):
self.font_size += 1
self.main_text.setFontPointSize(self.font_size)
self.main_text.setText(self.content)
def font_small(self):
self.font_size -= 1
self.main_text.setFontPointSize(self.font_size)
self.main_text.setText(self.content)
def update_back(self):
random_color = QColor(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
palette = QPalette()
palette.setColor(QPalette.Base, random_color)
self.main_text.setPalette(palette)
if __name__ == "__main__":
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()
|