基于Python自制一個(gè)資源管理器
本文為大家詳細(xì)介紹了如何基于Python制作一個(gè)資源管理器,可以進(jìn)行簡(jiǎn)單的本地資源管理,有需要的可以參考下
最終效果圖
完整代碼
import sys from PyQt5.QtWidgets import ( QApplication, QMainWindow, QTreeView, QFileSystemModel, QSplitter, QTextEdit, QVBoxLayout, QWidget, QPushButton, QListWidget, QListWidgetItem, QHBoxLayout ) from PyQt5.QtCore import QDir, QFileInfo from PyQt5.QtGui import QIcon class ResourceBrowser(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("資源瀏覽器") self.setGeometry(100, 100, 800, 600) self.setStyleSheet(""" QMainWindow { background-color: #2E3440; } QListWidget { background-color: #3B4252; color: #ECEFF4; border: 1px solid #4C566A; } QPushButton { background-color: #81A1C1; color: #ECEFF4; padding: 5px; border: none; } QPushButton:hover { background-color: #5E81AC; } """) # 當(dāng)前目錄路徑 self.current_dir = QDir.rootPath() # 創(chuàng)建主布局 main_widget = QWidget() self.setCentralWidget(main_widget) layout = QVBoxLayout(main_widget) # 創(chuàng)建分割器 splitter = QSplitter() layout.addWidget(splitter) # 左側(cè)布局 left_widget = QWidget() left_layout = QVBoxLayout(left_widget) # 返回上一級(jí)按鈕 self.btn_back = QPushButton("返回上一級(jí)") self.btn_back.clicked.connect(self.go_back) left_layout.addWidget(self.btn_back) # 創(chuàng)建文件系統(tǒng)模型 self.model = QFileSystemModel() self.model.setRootPath("") # 設(shè)置根路徑為空,顯示所有磁盤 # 創(chuàng)建樹視圖 self.tree_view = QTreeView() self.tree_view.setModel(self.model) self.tree_view.setRootIndex(self.model.index("")) # 設(shè)置根目錄為空,顯示所有磁盤 self.tree_view.clicked.connect(self.on_item_clicked) # 點(diǎn)擊事件 left_layout.addWidget(self.tree_view) # 將左側(cè)布局添加到分割器 splitter.addWidget(left_widget) # 創(chuàng)建右側(cè)布局 right_widget = QWidget() right_layout = QVBoxLayout(right_widget) # 創(chuàng)建右側(cè)文件列表 self.file_list = QListWidget() self.file_list.itemDoubleClicked.connect(self.on_file_double_clicked) # 雙擊文件事件 right_layout.addWidget(self.file_list) # 創(chuàng)建文本編輯框用于顯示文件內(nèi)容 self.text_edit = QTextEdit() self.text_edit.setReadOnly(True) # 設(shè)置為只讀 right_layout.addWidget(self.text_edit) # 將右側(cè)布局添加到分割器 splitter.addWidget(right_widget) # 設(shè)置分割器初始比例 splitter.setSizes([300, 500]) def on_item_clicked(self, index): """處理樹視圖項(xiàng)的點(diǎn)擊事件""" file_path = self.model.filePath(index) if self.model.isDir(index): # 如果是文件夾,更新當(dāng)前目錄并顯示文件列表 self.current_dir = file_path self.update_file_list(file_path) def update_file_list(self, dir_path): """更新右側(cè)文件列表""" self.file_list.clear() dir = QDir(dir_path) for file_info in dir.entryInfoList(QDir.Files | QDir.Dirs | QDir.NoDotAndDotDot): item = QListWidgetItem(file_info.fileName()) if file_info.isDir(): item.setIcon(QIcon.fromTheme("folder")) # 文件夾圖標(biāo) else: item.setIcon(QIcon.fromTheme("text-x-generic")) # 文件圖標(biāo) self.file_list.addItem(item) def on_file_double_clicked(self, item): """處理文件列表項(xiàng)的雙擊事件""" file_name = item.text() file_path = QDir(self.current_dir).filePath(file_name) if QFileInfo(file_path).isDir(): # 如果是文件夾,更新當(dāng)前目錄并顯示文件列表 self.current_dir = file_path self.update_file_list(file_path) else: # 如果是文件,顯示文件內(nèi)容 try: with open(file_path, "r", encoding="utf-8") as file: self.text_edit.setPlainText(file.read()) except Exception as e: self.text_edit.setPlainText(f"無法讀取文件: {str(e)}") def go_back(self): """返回上一級(jí)目錄""" current_dir = QDir(self.current_dir) if current_dir.cdUp(): # 嘗試返回上一級(jí)目錄 self.current_dir = current_dir.absolutePath() # 獲取上一級(jí)目錄的絕對(duì)路徑 self.update_file_list(self.current_dir) # 更新樹視圖的選中狀態(tài) index = self.model.index(self.current_dir) self.tree_view.setCurrentIndex(index) if __name__ == "__main__": app = QApplication(sys.argv) browser = ResourceBrowser() browser.show() sys.exit(app.exec())
到此這篇關(guān)于基于Python自制一個(gè)資源管理器的文章就介紹到這了,更多相關(guān)Python資源管理器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python基礎(chǔ)進(jìn)階之海量表情包多線程爬蟲功能的實(shí)現(xiàn)
這篇文章主要介紹了Python基礎(chǔ)進(jìn)階之海量表情包多線程爬蟲,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12Win10里python3創(chuàng)建虛擬環(huán)境的步驟
在本篇文章里小編給大家整理的是一篇關(guān)于Win10里python3創(chuàng)建虛擬環(huán)境的步驟內(nèi)容,需要的朋友們可以學(xué)習(xí)參考下。2020-01-01Pytest?fixture及conftest相關(guān)詳解
這篇文章主要介紹了Pytest?fixture及conftest相關(guān)詳解,fixture是在測(cè)試函數(shù)運(yùn)行前后,由pytest執(zhí)行的外殼函數(shù),更多相關(guān)內(nèi)容需要的朋友可以參考一下2022-09-09Django框架之登錄后自定義跳轉(zhuǎn)頁面的實(shí)現(xiàn)方法
這篇文章主要介紹了Django框架之登錄后自定義跳轉(zhuǎn)頁面的實(shí)現(xiàn)方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07