本文的文字及圖片來(lái)源于網(wǎng)絡(luò),僅供學(xué)習(xí)、交流使用,不具有任何商業(yè)用途,版權(quán)歸原作者所有,如有問(wèn)題請(qǐng)及時(shí)聯(lián)系我們以作處理 本文章來(lái)自騰訊云 作者:Python進(jìn)階者 想要學(xué)習(xí)Python?有問(wèn)題得不到第一時(shí)間解決?來(lái)看看這里“1039649593”滿足你的需求,資料都已經(jīng)上傳至文件中,可以自行下載!還有海量最新2020python學(xué)習(xí)資料。
class Book: def __init__(self, fname): # 文件名 self.fname = fname # 是否被閱讀 self.flag = None self._info = None self._page = 0 self.get_meta_data(self.fname) def __eq__(self, other): if hasattr(other, 'fname'): return self.fname == other.fname return False ? 同時(shí)閱讀 第二行代碼,是對(duì) read_list 進(jìn)行初始化。book.flag 用來(lái)判斷這本書(shū)上次關(guān)閉前是否處于閱讀的狀態(tài)。如果是,我們就把它放在閱讀列表中。 self.read_list = [None] self.read_list.extend(book for book in self.booklist if book.flag) ? 左鍵翻頁(yè) width 為 MyArea 區(qū)域的寬度,如果點(diǎn)擊鼠標(biāo)左鍵,且鼠標(biāo)位置的橫坐標(biāo)小于 1/3 區(qū)域?qū)挾?,那么向前翻?yè);大于 2/3 區(qū)域?qū)挾?,那么向后翻?yè)。 # 鼠標(biāo)左鍵翻頁(yè) def mousePressEvent(self, event): pos = event.pos().x() width = self.size().width() if event.button() == Qt.LeftButton: if pos > width * 2 / 3: self.right() elif pos < width / 3: self.left() sqlite3 ? sqlite3 是輕量型本地?cái)?shù)據(jù)庫(kù),具有無(wú)服務(wù)器、零配置、速度快等特點(diǎn)。 PyReadon 啟動(dòng)時(shí),會(huì)從數(shù)據(jù)庫(kù)中讀取圖書(shū)信息。read_db 函數(shù)主要執(zhí)行以下功能: 如果路徑中不存在 PDF.db 數(shù)據(jù)庫(kù),那么就新建 PDF.db 數(shù)據(jù)庫(kù),并且創(chuàng)建一個(gè) book_info 表格,該表格擁有三個(gè)屬性 path, page, flag; 從 book_info 表格中讀取數(shù)據(jù),并創(chuàng)建 book 對(duì)象來(lái)接收這些數(shù)據(jù),最后通過(guò) yield 函數(shù)返回 book 對(duì)象。 book_db = 'PDF.db' book_info = namedtuple('info', 'path page flag') def read_db(): # 將路徑更改為該文件所處路徑 os.chdir(os.path.dirname(os.path.realpath(__file__))) if not os.path.exists(book_db): conn = sqlite3.connect(book_db) conn.execute("CREATE TABLE book_info(path, page, flag)") conn.close() conn = sqlite3.connect(book_db) for row in conn.execute('SELECT * FROM book_info'): info = book_info(*row) book = Book(info.path) book.page = info.page book.flag = info.flag yield book conn.close() ? 將數(shù)據(jù)存儲(chǔ)到數(shù)據(jù)庫(kù)中: 將書(shū)籍列表傳給 save2db 函數(shù),通過(guò)列表推導(dǎo)式創(chuàng)建 book 所在地址的列表。conn.executemany 函數(shù)將迭代生成器表達(dá)式,并獲得 書(shū)籍地址、閱讀頁(yè)數(shù)、是否在閱讀列表中 等信息,最后將這些信息存儲(chǔ)在數(shù)據(jù)庫(kù)中。 def save2db(booklist): conn = sqlite3.connect(book_db) conn.executemany("INSERT INTO book_info Values (?,?,?)", ((book.fname, book.page, book.flag) for book in booklist)) conn.commit() conn.close() ? 在進(jìn)行存儲(chǔ)數(shù)據(jù)之前,我們首先要將 book_info 數(shù)據(jù)庫(kù)中的內(nèi)容清空。 def remove_db(): conn = sqlite3.connect(book_db) conn.execute('DELETE FROM book_info') conn.commit() conn.close() ? 查看書(shū)籍信息 elif action == item3: index = row_num * 8 col_num # 之后改成 book book = self.booklist[index] info = book.info fmt = f'路徑:{info.path}\n\n' f'格式:{info.format}\n\n' f'標(biāo)題:{info.title}\n\n' f'作者:{info.author}\n\n' f'Creator:{info.creator}\n\n' f'Producer:{info.producer}\n\n' QMessageBox.about(self, '文檔信息', fmt) ? 彈窗 通過(guò) Qt Designer 設(shè)計(jì)了一個(gè)彈窗,并與主程序綁定: info 即為彈窗,點(diǎn)擊工具欄中的信息欄時(shí)會(huì)彈出窗口。 info = Info() reader.infobar.triggered.connect(info.show) ? 來(lái)源:https://www./content-1-827051.html |
|
來(lái)自: 印度阿三17 > 《開(kāi)發(fā)》